Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add logic to set tag for instance name #10

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 17 additions & 22 deletions tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -93,31 +94,37 @@ 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
organization *resource.Organization
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
Expand All @@ -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
Expand All @@ -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 ""
Expand Down
76 changes: 28 additions & 48 deletions tags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
},
Expand All @@ -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",
Expand All @@ -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",
},
Expand All @@ -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",
Expand All @@ -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",
},
Expand All @@ -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",
Expand All @@ -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",
},
Expand All @@ -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",
Expand All @@ -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",
},
Expand All @@ -269,6 +284,7 @@ func TestGenerateTags(t *testing.T) {
organizationName: "org-1",
spaceName: "space-1",
instanceGUID: "abc5",
instanceName: "abc6",
},
},
expectedGetSpaceInstanceCallCount: 1,
Expand All @@ -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",
},
Expand All @@ -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",
Expand All @@ -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",
},
},
Expand All @@ -328,17 +347,19 @@ 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",
"environment": "testing",
"Service offering name": "abc1",
"Service plan name": "abc2",
"Instance GUID": "abc5",
"Instance name": "abc6",
},
},
}
Expand Down Expand Up @@ -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
Expand Down
Loading