github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/api/cloudcontroller/ccv3/relationship_list.go (about)

     1  package ccv3
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  
     7  	"code.cloudfoundry.org/cli/api/cloudcontroller"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/internal"
     9  )
    10  
    11  // RelationshipList represents a one to many relationship.
    12  type RelationshipList struct {
    13  	GUIDs []string
    14  }
    15  
    16  func (r RelationshipList) MarshalJSON() ([]byte, error) {
    17  	var ccRelationship struct {
    18  		Data []map[string]string `json:"data"`
    19  	}
    20  
    21  	for _, guid := range r.GUIDs {
    22  		ccRelationship.Data = append(
    23  			ccRelationship.Data,
    24  			map[string]string{
    25  				"guid": guid,
    26  			})
    27  	}
    28  
    29  	return json.Marshal(ccRelationship)
    30  }
    31  
    32  func (r *RelationshipList) UnmarshalJSON(data []byte) error {
    33  	var ccRelationships struct {
    34  		Data []map[string]string `json:"data"`
    35  	}
    36  
    37  	err := json.Unmarshal(data, &ccRelationships)
    38  	if err != nil {
    39  		return err
    40  	}
    41  
    42  	for _, partner := range ccRelationships.Data {
    43  		r.GUIDs = append(r.GUIDs, partner["guid"])
    44  	}
    45  	return nil
    46  }
    47  
    48  // EntitleIsolationSegmentToOrganizations will create a link between the
    49  // isolation segment and the list of organizations provided.
    50  func (client *Client) EntitleIsolationSegmentToOrganizations(isolationSegmentGUID string, organizationGUIDs []string) (RelationshipList, Warnings, error) {
    51  	body, err := json.Marshal(RelationshipList{GUIDs: organizationGUIDs})
    52  	if err != nil {
    53  		return RelationshipList{}, nil, err
    54  	}
    55  
    56  	request, err := client.newHTTPRequest(requestOptions{
    57  		RequestName: internal.PostIsolationSegmentRelationshipOrganizationsRequest,
    58  		URIParams:   internal.Params{"isolation_segment_guid": isolationSegmentGUID},
    59  		Body:        bytes.NewReader(body),
    60  	})
    61  	if err != nil {
    62  		return RelationshipList{}, nil, err
    63  	}
    64  
    65  	var relationships RelationshipList
    66  	response := cloudcontroller.Response{
    67  		Result: &relationships,
    68  	}
    69  
    70  	err = client.connection.Make(request, &response)
    71  	return relationships, response.Warnings, err
    72  }
    73  
    74  // ShareServiceInstanceToSpaces will create a sharing relationship between
    75  // the service instance and the shared-to space for each space provided.
    76  func (client *Client) ShareServiceInstanceToSpaces(serviceInstanceGUID string, spaceGUIDs []string) (RelationshipList, Warnings, error) {
    77  	body, err := json.Marshal(RelationshipList{GUIDs: spaceGUIDs})
    78  	if err != nil {
    79  		return RelationshipList{}, nil, err
    80  	}
    81  
    82  	request, err := client.newHTTPRequest(requestOptions{
    83  		RequestName: internal.PostServiceInstanceRelationshipsSharedSpacesRequest,
    84  		URIParams:   internal.Params{"service_instance_guid": serviceInstanceGUID},
    85  		Body:        bytes.NewReader(body),
    86  	})
    87  
    88  	if err != nil {
    89  		return RelationshipList{}, nil, err
    90  	}
    91  
    92  	var relationships RelationshipList
    93  	response := cloudcontroller.Response{
    94  		Result: &relationships,
    95  	}
    96  
    97  	err = client.connection.Make(request, &response)
    98  	return relationships, response.Warnings, err
    99  }