github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/api/cloudcontroller/ccv3/service_instance.go (about)

     1  package ccv3
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     5  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
     6  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/internal"
     7  	"code.cloudfoundry.org/cli/resources"
     8  	"code.cloudfoundry.org/cli/types"
     9  	"code.cloudfoundry.org/cli/util/lookuptable"
    10  )
    11  
    12  type SpaceWithOrganization struct {
    13  	SpaceGUID        string
    14  	SpaceName        string
    15  	OrganizationName string
    16  }
    17  
    18  // GetServiceInstances lists service instances with optional filters.
    19  func (client *Client) GetServiceInstances(query ...Query) ([]resources.ServiceInstance, IncludedResources, Warnings, error) {
    20  	var result []resources.ServiceInstance
    21  
    22  	included, warnings, err := client.MakeListRequest(RequestParams{
    23  		RequestName:  internal.GetServiceInstancesRequest,
    24  		Query:        query,
    25  		ResponseBody: resources.ServiceInstance{},
    26  		AppendToList: func(item interface{}) error {
    27  			result = append(result, item.(resources.ServiceInstance))
    28  			return nil
    29  		},
    30  	})
    31  
    32  	return result, included, warnings, err
    33  }
    34  
    35  func (client *Client) GetServiceInstanceByNameAndSpace(name, spaceGUID string, query ...Query) (resources.ServiceInstance, IncludedResources, Warnings, error) {
    36  	query = append(query,
    37  		Query{Key: NameFilter, Values: []string{name}},
    38  		Query{Key: SpaceGUIDFilter, Values: []string{spaceGUID}},
    39  		Query{Key: PerPage, Values: []string{"1"}},
    40  		Query{Key: Page, Values: []string{"1"}},
    41  	)
    42  
    43  	instances, included, warnings, err := client.GetServiceInstances(query...)
    44  
    45  	if err != nil {
    46  		return resources.ServiceInstance{}, IncludedResources{}, warnings, err
    47  	}
    48  
    49  	if len(instances) == 0 {
    50  		return resources.ServiceInstance{},
    51  			IncludedResources{},
    52  			warnings,
    53  			ccerror.ServiceInstanceNotFoundError{
    54  				Name:      name,
    55  				SpaceGUID: spaceGUID,
    56  			}
    57  	}
    58  
    59  	return instances[0], included, warnings, nil
    60  }
    61  
    62  func (client *Client) GetServiceInstanceParameters(serviceInstanceGUID string) (parameters types.JSONObject, warnings Warnings, err error) {
    63  	_, warnings, err = client.MakeRequest(RequestParams{
    64  		RequestName:  internal.GetServiceInstanceParametersRequest,
    65  		URIParams:    internal.Params{"service_instance_guid": serviceInstanceGUID},
    66  		ResponseBody: &parameters,
    67  	})
    68  
    69  	return
    70  }
    71  
    72  func (client *Client) CreateServiceInstance(serviceInstance resources.ServiceInstance) (JobURL, Warnings, error) {
    73  	return client.MakeRequest(RequestParams{
    74  		RequestName: internal.PostServiceInstanceRequest,
    75  		RequestBody: serviceInstance,
    76  	})
    77  }
    78  
    79  func (client *Client) UpdateServiceInstance(serviceInstanceGUID string, serviceInstanceUpdates resources.ServiceInstance) (JobURL, Warnings, error) {
    80  	return client.MakeRequest(RequestParams{
    81  		RequestName: internal.PatchServiceInstanceRequest,
    82  		URIParams:   internal.Params{"service_instance_guid": serviceInstanceGUID},
    83  		RequestBody: serviceInstanceUpdates,
    84  	})
    85  }
    86  
    87  func (client *Client) DeleteServiceInstance(serviceInstanceGUID string, query ...Query) (JobURL, Warnings, error) {
    88  	return client.MakeRequest(RequestParams{
    89  		RequestName: internal.DeleteServiceInstanceRequest,
    90  		URIParams:   internal.Params{"service_instance_guid": serviceInstanceGUID},
    91  		Query:       query,
    92  	})
    93  }
    94  
    95  // ShareServiceInstanceToSpaces will create a sharing relationship between
    96  // the service instance and the shared-to space for each space provided.
    97  func (client *Client) ShareServiceInstanceToSpaces(serviceInstanceGUID string, spaceGUIDs []string) (resources.RelationshipList, Warnings, error) {
    98  	var responseBody resources.RelationshipList
    99  
   100  	_, warnings, err := client.MakeRequest(RequestParams{
   101  		RequestName:  internal.PostServiceInstanceRelationshipsSharedSpacesRequest,
   102  		URIParams:    internal.Params{"service_instance_guid": serviceInstanceGUID},
   103  		RequestBody:  resources.RelationshipList{GUIDs: spaceGUIDs},
   104  		ResponseBody: &responseBody,
   105  	})
   106  
   107  	return responseBody, warnings, err
   108  }
   109  
   110  // UnshareServiceInstanceFromSpace will delete the sharing relationship
   111  // between the service instance and the shared-to space provided.
   112  func (client *Client) UnshareServiceInstanceFromSpace(serviceInstanceGUID string, spaceGUID string) (Warnings, error) {
   113  	_, warnings, err := client.MakeRequest(RequestParams{
   114  		RequestName: internal.DeleteServiceInstanceRelationshipsSharedSpaceRequest,
   115  		URIParams:   internal.Params{"service_instance_guid": serviceInstanceGUID, "space_guid": spaceGUID},
   116  	})
   117  
   118  	return warnings, err
   119  }
   120  
   121  // GetServiceInstanceSharedSpaces will fetch relationships between
   122  // a service instance and the shared-to spaces for that service.
   123  func (client *Client) GetServiceInstanceSharedSpaces(serviceInstanceGUID string) ([]SpaceWithOrganization, Warnings, error) {
   124  	var responseBody resources.SharedToSpacesListWrapper
   125  	query := []Query{
   126  		{
   127  			Key:    FieldsSpace,
   128  			Values: []string{"guid", "name", "relationships.organization"},
   129  		},
   130  		{
   131  			Key:    FieldsSpaceOrganization,
   132  			Values: []string{"guid", "name"},
   133  		},
   134  	}
   135  	_, warnings, err := client.MakeRequest(RequestParams{
   136  		RequestName:  internal.GetServiceInstanceRelationshipsSharedSpacesRequest,
   137  		URIParams:    internal.Params{"service_instance_guid": serviceInstanceGUID},
   138  		Query:        query,
   139  		ResponseBody: &responseBody,
   140  	})
   141  	return mapRelationshipsToSpaces(responseBody), warnings, err
   142  }
   143  
   144  func (client *Client) GetServiceInstanceUsageSummary(serviceInstanceGUID string) ([]resources.ServiceInstanceUsageSummary, Warnings, error) {
   145  	var result resources.ServiceInstanceUsageSummaryList
   146  
   147  	_, warnings, err := client.MakeRequest(RequestParams{
   148  		RequestName:  internal.GetServiceInstanceSharedSpacesUsageSummaryRequest,
   149  		URIParams:    internal.Params{"service_instance_guid": serviceInstanceGUID},
   150  		ResponseBody: &result,
   151  	})
   152  	return result.UsageSummary, warnings, err
   153  }
   154  
   155  func mapRelationshipsToSpaces(sharedToSpaces resources.SharedToSpacesListWrapper) []SpaceWithOrganization {
   156  	var spacesToReturn []SpaceWithOrganization
   157  
   158  	guidToOrgNameLookup := lookuptable.NameFromGUID(sharedToSpaces.Organizations)
   159  
   160  	for _, s := range sharedToSpaces.Spaces {
   161  		org := s.Relationships[constant.RelationshipTypeOrganization]
   162  		space := SpaceWithOrganization{SpaceGUID: s.GUID, SpaceName: s.Name, OrganizationName: guidToOrgNameLookup[org.GUID]}
   163  		spacesToReturn = append(spacesToReturn, space)
   164  	}
   165  
   166  	return spacesToReturn
   167  }