github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/actor/v7action/label.go (about) 1 package v7action 2 3 import ( 4 "code.cloudfoundry.org/cli/actor/actionerror" 5 "code.cloudfoundry.org/cli/resources" 6 "code.cloudfoundry.org/cli/types" 7 ) 8 9 func (actor *Actor) GetApplicationLabels(appName string, spaceGUID string) (map[string]types.NullString, Warnings, error) { 10 resource, warnings, err := actor.GetApplicationByNameAndSpace(appName, spaceGUID) 11 return actor.extractLabels((*resources.Metadata)(resource.Metadata), warnings, err) 12 } 13 14 func (actor *Actor) GetDomainLabels(domainName string) (map[string]types.NullString, Warnings, error) { 15 resource, warnings, err := actor.GetDomainByName(domainName) 16 return actor.extractLabels(resource.Metadata, warnings, err) 17 } 18 19 func (actor *Actor) GetOrganizationLabels(orgName string) (map[string]types.NullString, Warnings, error) { 20 resource, warnings, err := actor.GetOrganizationByName(orgName) 21 return actor.extractLabels(resource.Metadata, warnings, err) 22 } 23 24 func (actor *Actor) GetRouteLabels(routeName string, spaceGUID string) (map[string]types.NullString, Warnings, error) { 25 resource, warnings, err := actor.GetRoute(routeName, spaceGUID) 26 return actor.extractLabels((*resources.Metadata)(resource.Metadata), warnings, err) 27 } 28 29 func (actor *Actor) GetServiceBrokerLabels(serviceBrokerName string) (map[string]types.NullString, Warnings, error) { 30 serviceBroker, warnings, err := actor.GetServiceBrokerByName(serviceBrokerName) 31 return actor.extractLabels(serviceBroker.Metadata, warnings, err) 32 } 33 34 func (actor *Actor) GetServiceInstanceLabels(serviceInstanceName, spaceGUID string) (map[string]types.NullString, Warnings, error) { 35 serviceInstance, warnings, err := actor.GetServiceInstanceByNameAndSpace(serviceInstanceName, spaceGUID) 36 return actor.extractLabels(serviceInstance.Metadata, warnings, err) 37 } 38 39 func (actor *Actor) GetServiceOfferingLabels(serviceOfferingName, serviceBrokerName string) (map[string]types.NullString, Warnings, error) { 40 serviceOffering, warnings, err := actor.CloudControllerClient.GetServiceOfferingByNameAndBroker(serviceOfferingName, serviceBrokerName) 41 return actor.extractLabels(serviceOffering.Metadata, Warnings(warnings), actionerror.EnrichAPIErrors(err)) 42 } 43 44 func (actor *Actor) GetServicePlanLabels(servicePlanName, serviceOfferingName, serviceBrokerName string) (map[string]types.NullString, Warnings, error) { 45 servicePlan, warnings, err := actor.GetServicePlanByNameOfferingAndBroker(servicePlanName, serviceOfferingName, serviceBrokerName) 46 return actor.extractLabels(servicePlan.Metadata, warnings, err) 47 } 48 49 func (actor *Actor) GetSpaceLabels(spaceName string, orgGUID string) (map[string]types.NullString, Warnings, error) { 50 resource, warnings, err := actor.GetSpaceByNameAndOrganization(spaceName, orgGUID) 51 return actor.extractLabels(resource.Metadata, warnings, err) 52 } 53 54 func (actor *Actor) GetStackLabels(stackName string) (map[string]types.NullString, Warnings, error) { 55 resource, warnings, err := actor.GetStackByName(stackName) 56 return actor.extractLabels(resource.Metadata, warnings, err) 57 } 58 59 func (actor *Actor) GetBuildpackLabels(buildpackName string, buildpackStack string) (map[string]types.NullString, Warnings, error) { 60 resource, warnings, err := actor.GetBuildpackByNameAndStack(buildpackName, buildpackStack) 61 return actor.extractLabels(resource.Metadata, warnings, err) 62 } 63 64 func (actor *Actor) extractLabels(metadata *resources.Metadata, warnings Warnings, err error) (map[string]types.NullString, Warnings, error) { 65 var labels map[string]types.NullString 66 67 if err != nil { 68 return labels, warnings, err 69 } 70 if metadata != nil { 71 labels = metadata.Labels 72 } 73 return labels, warnings, nil 74 } 75 76 func (actor *Actor) UpdateApplicationLabelsByApplicationName(appName string, spaceGUID string, labels map[string]types.NullString) (Warnings, error) { 77 app, warnings, err := actor.GetApplicationByNameAndSpace(appName, spaceGUID) 78 if err != nil { 79 return warnings, err 80 } 81 return actor.updateResourceMetadata("app", app.GUID, resources.Metadata{Labels: labels}, warnings) 82 } 83 84 func (actor *Actor) UpdateBuildpackLabelsByBuildpackNameAndStack(buildpackName string, stack string, labels map[string]types.NullString) (Warnings, error) { 85 buildpack, warnings, err := actor.GetBuildpackByNameAndStack(buildpackName, stack) 86 if err != nil { 87 return warnings, err 88 } 89 return actor.updateResourceMetadata("buildpack", buildpack.GUID, resources.Metadata{Labels: labels}, warnings) 90 } 91 92 func (actor *Actor) UpdateDomainLabelsByDomainName(domainName string, labels map[string]types.NullString) (Warnings, error) { 93 domain, warnings, err := actor.GetDomainByName(domainName) 94 if err != nil { 95 return warnings, err 96 } 97 return actor.updateResourceMetadata("domain", domain.GUID, resources.Metadata{Labels: labels}, warnings) 98 } 99 100 func (actor *Actor) UpdateOrganizationLabelsByOrganizationName(orgName string, labels map[string]types.NullString) (Warnings, error) { 101 org, warnings, err := actor.GetOrganizationByName(orgName) 102 if err != nil { 103 return warnings, err 104 } 105 return actor.updateResourceMetadata("org", org.GUID, resources.Metadata{Labels: labels}, warnings) 106 } 107 108 func (actor *Actor) UpdateRouteLabels(routeName string, spaceGUID string, labels map[string]types.NullString) (Warnings, error) { 109 route, warnings, err := actor.GetRoute(routeName, spaceGUID) 110 if err != nil { 111 return warnings, err 112 } 113 return actor.updateResourceMetadata("route", route.GUID, resources.Metadata{Labels: labels}, warnings) 114 } 115 116 func (actor *Actor) UpdateSpaceLabelsBySpaceName(spaceName string, orgGUID string, labels map[string]types.NullString) (Warnings, error) { 117 space, warnings, err := actor.GetSpaceByNameAndOrganization(spaceName, orgGUID) 118 if err != nil { 119 return warnings, err 120 } 121 return actor.updateResourceMetadata("space", space.GUID, resources.Metadata{Labels: labels}, warnings) 122 } 123 124 func (actor *Actor) UpdateStackLabelsByStackName(stackName string, labels map[string]types.NullString) (Warnings, error) { 125 stack, warnings, err := actor.GetStackByName(stackName) 126 if err != nil { 127 return warnings, err 128 } 129 return actor.updateResourceMetadata("stack", stack.GUID, resources.Metadata{Labels: labels}, warnings) 130 } 131 132 func (actor *Actor) UpdateServiceBrokerLabelsByServiceBrokerName(serviceBrokerName string, labels map[string]types.NullString) (Warnings, error) { 133 serviceBroker, warnings, err := actor.GetServiceBrokerByName(serviceBrokerName) 134 if err != nil { 135 return warnings, err 136 } 137 return actor.updateResourceMetadata("service-broker", serviceBroker.GUID, resources.Metadata{Labels: labels}, warnings) 138 } 139 140 func (actor *Actor) UpdateServiceInstanceLabels(serviceInstanceName, spaceGUID string, labels map[string]types.NullString) (Warnings, error) { 141 serviceInstance, warnings, err := actor.GetServiceInstanceByNameAndSpace(serviceInstanceName, spaceGUID) 142 if err != nil { 143 return warnings, err 144 } 145 return actor.updateResourceMetadata("service-instance", serviceInstance.GUID, resources.Metadata{Labels: labels}, warnings) 146 } 147 148 func (actor *Actor) UpdateServiceOfferingLabels(serviceOfferingName string, serviceBrokerName string, labels map[string]types.NullString) (Warnings, error) { 149 serviceOffering, warnings, err := actor.CloudControllerClient.GetServiceOfferingByNameAndBroker(serviceOfferingName, serviceBrokerName) 150 if err != nil { 151 return Warnings(warnings), actionerror.EnrichAPIErrors(err) 152 } 153 return actor.updateResourceMetadata("service-offering", serviceOffering.GUID, resources.Metadata{Labels: labels}, Warnings(warnings)) 154 } 155 156 func (actor *Actor) UpdateServicePlanLabels(servicePlanName string, serviceOfferingName string, serviceBrokerName string, labels map[string]types.NullString) (Warnings, error) { 157 servicePlan, warnings, err := actor.GetServicePlanByNameOfferingAndBroker(servicePlanName, serviceOfferingName, serviceBrokerName) 158 if err != nil { 159 return warnings, err 160 } 161 return actor.updateResourceMetadata("service-plan", servicePlan.GUID, resources.Metadata{Labels: labels}, warnings) 162 } 163 164 func (actor *Actor) updateResourceMetadata(resourceType string, resourceGUID string, payload resources.Metadata, warnings Warnings) (Warnings, error) { 165 jobURL, updateWarnings, err := actor.CloudControllerClient.UpdateResourceMetadata(resourceType, resourceGUID, payload) 166 warnings = append(warnings, updateWarnings...) 167 if err != nil { 168 return warnings, err 169 } 170 171 if jobURL != "" { 172 pollWarnings, err := actor.CloudControllerClient.PollJob(jobURL) 173 warnings = append(warnings, pollWarnings...) 174 if err != nil { 175 return warnings, err 176 } 177 } 178 179 return warnings, nil 180 }