diff --git a/tags.go b/tags.go index 3986a13..cbe731f 100644 --- a/tags.go +++ b/tags.go @@ -14,6 +14,7 @@ const ( OrganizationGUIDTagKey = "Organization GUID" OrganizationNameTagKey = "Organization name" ServiceInstanceGUIDTagKey = "Instance GUID" + ServiceInstanceNameTagKey = "Instance name" ServiceNameTagKey = "Service offering name" ServicePlanName = "Service plan name" SpaceGUIDTagKey = "Space GUID" @@ -93,11 +94,9 @@ func (t *CfTagManager) GenerateTags( tags[ServicePlanName] = planName } - if resourceGUIDs.InstanceGUID != "" { - tags[ServiceInstanceGUIDTagKey] = resourceGUIDs.InstanceGUID - } - var ( + instanceGUID string + instance *resource.ServiceInstance spaceGUID string space *resource.Space organizationGUID string @@ -105,19 +104,27 @@ func (t *CfTagManager) GenerateTags( err error ) - spaceGUID = resourceGUIDs.SpaceGUID - if spaceGUID == "" && getMissingResources { - spaceGUID, err = t.getSpaceGuid(resourceGUIDs.InstanceGUID) + instanceGUID = resourceGUIDs.InstanceGUID + if instanceGUID != "" { + tags[ServiceInstanceGUIDTagKey] = instanceGUID + + instance, err = t.cfResourceGetter.getServiceInstance(instanceGUID) if err != nil { return nil, err } } - if spaceGUID != "" { - tags[SpaceGUIDTagKey] = spaceGUID + if instance != nil { + tags[ServiceInstanceNameTagKey] = instance.Name + } + + spaceGUID = resourceGUIDs.SpaceGUID + if spaceGUID == "" && instance != nil { + spaceGUID = instance.Relationships.Space.Data.GUID } - if spaceGUID != "" && space == nil { + if spaceGUID != "" { + tags[SpaceGUIDTagKey] = spaceGUID space, err = t.cfResourceGetter.getSpace(spaceGUID) if err != nil { return nil, err @@ -135,9 +142,6 @@ func (t *CfTagManager) GenerateTags( if organizationGUID != "" { tags[OrganizationGUIDTagKey] = organizationGUID - } - - if organizationGUID != "" && organization == nil { organization, err = t.cfResourceGetter.getOrganization(organizationGUID) if err != nil { return nil, err @@ -151,15 +155,6 @@ func (t *CfTagManager) GenerateTags( return tags, nil } -func (t *CfTagManager) getSpaceGuid(instanceGUID string) (string, error) { - instance, err := t.cfResourceGetter.getServiceInstance(instanceGUID) - if err != nil { - return "", err - } - spaceGUID := instance.Relationships.Space.Data.GUID - return spaceGUID, nil -} - func (t *CfTagManager) getOrganizationGuidFromSpace(space *resource.Space) string { if space == nil { return "" diff --git a/tags_test.go b/tags_test.go index 3cce4cf..2319e61 100644 --- a/tags_test.go +++ b/tags_test.go @@ -104,9 +104,11 @@ func TestGenerateTags(t *testing.T) { spaceGUID: "abc4", organizationGUID: "abc3", instanceGUID: "abc5", + instanceName: "abc6", }, }, - expectedGetSpaceInstanceCallCount: 1, + expectedGetServiceInstanceCallCount: 1, + expectedGetSpaceInstanceCallCount: 1, expectedTags: map[string]string{ "client": "Cloud Foundry", "broker": "AWS Broker", @@ -116,6 +118,7 @@ func TestGenerateTags(t *testing.T) { "Organization GUID": "abc3", "Space GUID": "abc4", "Instance GUID": "abc5", + "Instance name": "abc6", "Organization name": "org-1", "Space name": "space-1", }, @@ -138,9 +141,11 @@ func TestGenerateTags(t *testing.T) { spaceGUID: "abc4", organizationGUID: "abc3", instanceGUID: "abc5", + instanceName: "abc6", }, }, - expectedGetSpaceInstanceCallCount: 1, + expectedGetServiceInstanceCallCount: 1, + expectedGetSpaceInstanceCallCount: 1, expectedTags: map[string]string{ "client": "Cloud Foundry", "broker": "AWS Broker", @@ -150,6 +155,7 @@ func TestGenerateTags(t *testing.T) { "Organization GUID": "abc3", "Space GUID": "abc4", "Instance GUID": "abc5", + "Instance name": "abc6", "Organization name": "org-1", "Space name": "space-1", }, @@ -171,9 +177,11 @@ func TestGenerateTags(t *testing.T) { spaceGUID: "abc4", organizationGUID: "abc3", instanceGUID: "abc5", + instanceName: "abc6", }, }, - expectedGetSpaceInstanceCallCount: 1, + expectedGetServiceInstanceCallCount: 1, + expectedGetSpaceInstanceCallCount: 1, expectedTags: map[string]string{ "client": "Cloud Foundry", "environment": "testing", @@ -182,6 +190,7 @@ func TestGenerateTags(t *testing.T) { "Organization GUID": "abc3", "Space GUID": "abc4", "Instance GUID": "abc5", + "Instance name": "abc6", "Organization name": "org-1", "Space name": "space-1", }, @@ -203,9 +212,11 @@ func TestGenerateTags(t *testing.T) { spaceGUID: "abc4", organizationGUID: "abc3", instanceGUID: "abc5", + instanceName: "abc6", }, }, - expectedGetSpaceInstanceCallCount: 1, + expectedGetServiceInstanceCallCount: 1, + expectedGetSpaceInstanceCallCount: 1, expectedTags: map[string]string{ "client": "Cloud Foundry", "broker": "AWS Broker", @@ -214,6 +225,7 @@ func TestGenerateTags(t *testing.T) { "Organization GUID": "abc3", "Space GUID": "abc4", "Instance GUID": "abc5", + "Instance name": "abc6", "Organization name": "org-1", "Space name": "space-1", }, @@ -236,9 +248,11 @@ func TestGenerateTags(t *testing.T) { spaceName: "space-1", spaceGUID: "abc4", instanceGUID: "abc5", + instanceName: "abc6", }, }, - expectedGetSpaceInstanceCallCount: 1, + expectedGetServiceInstanceCallCount: 1, + expectedGetSpaceInstanceCallCount: 1, expectedTags: map[string]string{ "client": "Cloud Foundry", "broker": "AWS Broker", @@ -248,6 +262,7 @@ func TestGenerateTags(t *testing.T) { "Organization GUID": "abc3", "Space GUID": "abc4", "Instance GUID": "abc5", + "Instance name": "abc6", "Organization name": "org-1", "Space name": "space-1", }, @@ -269,6 +284,7 @@ func TestGenerateTags(t *testing.T) { organizationName: "org-1", spaceName: "space-1", instanceGUID: "abc5", + instanceName: "abc6", }, }, expectedGetSpaceInstanceCallCount: 1, @@ -282,6 +298,7 @@ func TestGenerateTags(t *testing.T) { "Organization GUID": "abc3", "Space GUID": "abc4", "Instance GUID": "abc5", + "Instance name": "abc6", "Organization name": "org-1", "Space name": "space-1", }, @@ -301,10 +318,11 @@ func TestGenerateTags(t *testing.T) { spaceGUID: "abc4", spaceName: "space-1", instanceGUID: "abc5", + instanceName: "abc6", }, }, expectedGetSpaceInstanceCallCount: 1, - expectedGetServiceInstanceCallCount: 0, + expectedGetServiceInstanceCallCount: 1, expectedTags: map[string]string{ "client": "Cloud Foundry", "broker": "AWS Broker", @@ -313,6 +331,7 @@ func TestGenerateTags(t *testing.T) { "Service plan name": "abc2", "Space GUID": "abc4", "Instance GUID": "abc5", + "Instance name": "abc6", "Space name": "space-1", }, }, @@ -328,10 +347,11 @@ func TestGenerateTags(t *testing.T) { environment: "testing", cfResourceGetter: &mockCFClientWrapper{ instanceGUID: "abc5", + instanceName: "abc6", }, }, expectedGetSpaceInstanceCallCount: 0, - expectedGetServiceInstanceCallCount: 0, + expectedGetServiceInstanceCallCount: 1, expectedTags: map[string]string{ "client": "Cloud Foundry", "broker": "AWS Broker", @@ -339,6 +359,7 @@ func TestGenerateTags(t *testing.T) { "Service offering name": "abc1", "Service plan name": "abc2", "Instance GUID": "abc5", + "Instance name": "abc6", }, }, } @@ -424,47 +445,6 @@ func TestGenerateTagsHandleErrors(t *testing.T) { } } -func TestGetSpaceGuid(t *testing.T) { - testCases := map[string]struct { - tagManager *CfTagManager - instanceGUID string - expectedGuid string - expectedErr error - }{ - "success": { - tagManager: &CfTagManager{ - cfResourceGetter: &mockCFClientWrapper{ - instanceGUID: "instance-1", - spaceGUID: "space-1", - }, - }, - instanceGUID: "instance-1", - expectedGuid: "space-1", - }, - "error": { - tagManager: &CfTagManager{ - cfResourceGetter: &mockCFClientWrapper{ - getServiceInstanceErr: errors.New("error getting service instance"), - }, - }, - expectedErr: errors.New("error getting service instance"), - }, - } - - for name, test := range testCases { - t.Run(name, func(t *testing.T) { - spaceGUID, err := test.tagManager.getSpaceGuid(test.instanceGUID) - if spaceGUID != test.expectedGuid { - t.Errorf("expected: %s, got: %s", test.expectedGuid, spaceGUID) - } - if (test.expectedErr != nil && err == nil) || - (err != nil && err.Error() != test.expectedErr.Error()) { - t.Errorf("expected error: %s, got: %s", test.expectedErr, err) - } - }) - } -} - func TestGetOrganizationGuidFromSpace(t *testing.T) { testCases := map[string]struct { space *resource.Space