github.com/Thanhphan1147/cloudfoundry-cli@v7.1.0+incompatible/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) GetServiceOfferingLabels(serviceOfferingName, serviceBrokerName string) (map[string]types.NullString, Warnings, error) {
    35  	serviceOffering, warnings, err := actor.CloudControllerClient.GetServiceOfferingByNameAndBroker(serviceOfferingName, serviceBrokerName)
    36  	return actor.extractLabels(serviceOffering.Metadata, Warnings(warnings), actionerror.EnrichAPIErrors(err))
    37  }
    38  
    39  func (actor Actor) GetServicePlanLabels(servicePlanName, serviceOfferingName, serviceBrokerName string) (map[string]types.NullString, Warnings, error) {
    40  	servicePlan, warnings, err := actor.GetServicePlanByNameOfferingAndBroker(servicePlanName, serviceOfferingName, serviceBrokerName)
    41  	return actor.extractLabels(servicePlan.Metadata, warnings, err)
    42  }
    43  
    44  func (actor *Actor) GetSpaceLabels(spaceName string, orgGUID string) (map[string]types.NullString, Warnings, error) {
    45  	resource, warnings, err := actor.GetSpaceByNameAndOrganization(spaceName, orgGUID)
    46  	return actor.extractLabels(resource.Metadata, warnings, err)
    47  }
    48  
    49  func (actor *Actor) GetStackLabels(stackName string) (map[string]types.NullString, Warnings, error) {
    50  	resource, warnings, err := actor.GetStackByName(stackName)
    51  	return actor.extractLabels(resource.Metadata, warnings, err)
    52  }
    53  
    54  func (actor *Actor) GetBuildpackLabels(buildpackName string, buildpackStack string) (map[string]types.NullString, Warnings, error) {
    55  	resource, warnings, err := actor.GetBuildpackByNameAndStack(buildpackName, buildpackStack)
    56  	return actor.extractLabels(resource.Metadata, warnings, err)
    57  }
    58  
    59  func (actor *Actor) extractLabels(metadata *resources.Metadata, warnings Warnings, err error) (map[string]types.NullString, Warnings, error) {
    60  	var labels map[string]types.NullString
    61  
    62  	if err != nil {
    63  		return labels, warnings, err
    64  	}
    65  	if metadata != nil {
    66  		labels = metadata.Labels
    67  	}
    68  	return labels, warnings, nil
    69  }
    70  
    71  func (actor *Actor) UpdateApplicationLabelsByApplicationName(appName string, spaceGUID string, labels map[string]types.NullString) (Warnings, error) {
    72  	app, warnings, err := actor.GetApplicationByNameAndSpace(appName, spaceGUID)
    73  	if err != nil {
    74  		return warnings, err
    75  	}
    76  	return actor.updateResourceMetadata("app", app.GUID, resources.Metadata{Labels: labels}, warnings)
    77  }
    78  
    79  func (actor *Actor) UpdateBuildpackLabelsByBuildpackNameAndStack(buildpackName string, stack string, labels map[string]types.NullString) (Warnings, error) {
    80  	buildpack, warnings, err := actor.GetBuildpackByNameAndStack(buildpackName, stack)
    81  	if err != nil {
    82  		return warnings, err
    83  	}
    84  	return actor.updateResourceMetadata("buildpack", buildpack.GUID, resources.Metadata{Labels: labels}, warnings)
    85  }
    86  
    87  func (actor *Actor) UpdateDomainLabelsByDomainName(domainName string, labels map[string]types.NullString) (Warnings, error) {
    88  	domain, warnings, err := actor.GetDomainByName(domainName)
    89  	if err != nil {
    90  		return warnings, err
    91  	}
    92  	return actor.updateResourceMetadata("domain", domain.GUID, resources.Metadata{Labels: labels}, warnings)
    93  }
    94  
    95  func (actor *Actor) UpdateOrganizationLabelsByOrganizationName(orgName string, labels map[string]types.NullString) (Warnings, error) {
    96  	org, warnings, err := actor.GetOrganizationByName(orgName)
    97  	if err != nil {
    98  		return warnings, err
    99  	}
   100  	return actor.updateResourceMetadata("org", org.GUID, resources.Metadata{Labels: labels}, warnings)
   101  }
   102  
   103  func (actor *Actor) UpdateRouteLabels(routeName string, spaceGUID string, labels map[string]types.NullString) (Warnings, error) {
   104  	route, warnings, err := actor.GetRoute(routeName, spaceGUID)
   105  	if err != nil {
   106  		return warnings, err
   107  	}
   108  	return actor.updateResourceMetadata("route", route.GUID, resources.Metadata{Labels: labels}, warnings)
   109  }
   110  
   111  func (actor *Actor) UpdateSpaceLabelsBySpaceName(spaceName string, orgGUID string, labels map[string]types.NullString) (Warnings, error) {
   112  	space, warnings, err := actor.GetSpaceByNameAndOrganization(spaceName, orgGUID)
   113  	if err != nil {
   114  		return warnings, err
   115  	}
   116  	return actor.updateResourceMetadata("space", space.GUID, resources.Metadata{Labels: labels}, warnings)
   117  }
   118  
   119  func (actor *Actor) UpdateStackLabelsByStackName(stackName string, labels map[string]types.NullString) (Warnings, error) {
   120  	stack, warnings, err := actor.GetStackByName(stackName)
   121  	if err != nil {
   122  		return warnings, err
   123  	}
   124  	return actor.updateResourceMetadata("stack", stack.GUID, resources.Metadata{Labels: labels}, warnings)
   125  }
   126  
   127  func (actor *Actor) UpdateServiceBrokerLabelsByServiceBrokerName(serviceBrokerName string, labels map[string]types.NullString) (Warnings, error) {
   128  	serviceBroker, warnings, err := actor.GetServiceBrokerByName(serviceBrokerName)
   129  	if err != nil {
   130  		return warnings, err
   131  	}
   132  	return actor.updateResourceMetadata("service-broker", serviceBroker.GUID, resources.Metadata{Labels: labels}, warnings)
   133  }
   134  
   135  func (actor *Actor) UpdateServiceOfferingLabels(serviceOfferingName string, serviceBrokerName string, labels map[string]types.NullString) (Warnings, error) {
   136  	serviceOffering, warnings, err := actor.CloudControllerClient.GetServiceOfferingByNameAndBroker(serviceOfferingName, serviceBrokerName)
   137  	if err != nil {
   138  		return Warnings(warnings), actionerror.EnrichAPIErrors(err)
   139  	}
   140  	return actor.updateResourceMetadata("service-offering", serviceOffering.GUID, resources.Metadata{Labels: labels}, Warnings(warnings))
   141  }
   142  
   143  func (actor *Actor) UpdateServicePlanLabels(servicePlanName string, serviceOfferingName string, serviceBrokerName string, labels map[string]types.NullString) (Warnings, error) {
   144  	servicePlan, warnings, err := actor.GetServicePlanByNameOfferingAndBroker(servicePlanName, serviceOfferingName, serviceBrokerName)
   145  	if err != nil {
   146  		return warnings, err
   147  	}
   148  	return actor.updateResourceMetadata("service-plan", servicePlan.GUID, resources.Metadata{Labels: labels}, warnings)
   149  }
   150  
   151  func (actor *Actor) updateResourceMetadata(resourceType string, resourceGUID string, payload resources.Metadata, warnings Warnings) (Warnings, error) {
   152  	jobURL, updateWarnings, err := actor.CloudControllerClient.UpdateResourceMetadata(resourceType, resourceGUID, payload)
   153  	warnings = append(warnings, updateWarnings...)
   154  	if err != nil {
   155  		return warnings, err
   156  	}
   157  
   158  	if jobURL != "" {
   159  		pollWarnings, err := actor.CloudControllerClient.PollJob(jobURL)
   160  		warnings = append(warnings, pollWarnings...)
   161  		if err != nil {
   162  			return warnings, err
   163  		}
   164  	}
   165  
   166  	return warnings, nil
   167  }