github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/api/cloudcontroller/ccv3/relationship.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  // Relationship represents a one to one relationship.
    12  // An empty GUID will be marshaled as `null`.
    13  type Relationship struct {
    14  	GUID string
    15  }
    16  
    17  func (r Relationship) MarshalJSON() ([]byte, error) {
    18  	if r.GUID == "" {
    19  		var emptyCCRelationship struct {
    20  			Data interface{} `json:"data"`
    21  		}
    22  		return json.Marshal(emptyCCRelationship)
    23  	}
    24  
    25  	var ccRelationship struct {
    26  		Data struct {
    27  			GUID string `json:"guid"`
    28  		} `json:"data"`
    29  	}
    30  
    31  	ccRelationship.Data.GUID = r.GUID
    32  	return json.Marshal(ccRelationship)
    33  }
    34  
    35  func (r *Relationship) UnmarshalJSON(data []byte) error {
    36  	var ccRelationship struct {
    37  		Data struct {
    38  			GUID string `json:"guid"`
    39  		} `json:"data"`
    40  	}
    41  
    42  	err := json.Unmarshal(data, &ccRelationship)
    43  	if err != nil {
    44  		return err
    45  	}
    46  
    47  	r.GUID = ccRelationship.Data.GUID
    48  	return nil
    49  }
    50  
    51  // AssignSpaceToIsolationSegment assigns an isolation segment to a space and
    52  // returns the relationship.
    53  func (client *Client) AssignSpaceToIsolationSegment(spaceGUID string, isolationSegmentGUID string) (Relationship, Warnings, error) {
    54  	body, err := json.Marshal(Relationship{GUID: isolationSegmentGUID})
    55  	if err != nil {
    56  		return Relationship{}, nil, err
    57  	}
    58  
    59  	request, err := client.newHTTPRequest(requestOptions{
    60  		RequestName: internal.PatchSpaceRelationshipIsolationSegmentRequest,
    61  		URIParams:   internal.Params{"space_guid": spaceGUID},
    62  		Body:        bytes.NewReader(body),
    63  	})
    64  	if err != nil {
    65  		return Relationship{}, nil, err
    66  	}
    67  
    68  	var relationship Relationship
    69  	response := cloudcontroller.Response{
    70  		Result: &relationship,
    71  	}
    72  
    73  	err = client.connection.Make(request, &response)
    74  	return relationship, response.Warnings, err
    75  }
    76  
    77  // GetSpaceIsolationSegment returns the relationship between a space and it's
    78  // isolation segment.
    79  func (client *Client) GetSpaceIsolationSegment(spaceGUID string) (Relationship, Warnings, error) {
    80  	request, err := client.newHTTPRequest(requestOptions{
    81  		RequestName: internal.GetSpaceRelationshipIsolationSegmentRequest,
    82  		URIParams:   internal.Params{"space_guid": spaceGUID},
    83  	})
    84  	if err != nil {
    85  		return Relationship{}, nil, err
    86  	}
    87  
    88  	var relationship Relationship
    89  	response := cloudcontroller.Response{
    90  		Result: &relationship,
    91  	}
    92  
    93  	err = client.connection.Make(request, &response)
    94  	return relationship, response.Warnings, err
    95  }
    96  
    97  // RevokeIsolationSegmentFromOrganization will delete the relationship between
    98  // the isolation segment and the organization provided.
    99  func (client *Client) RevokeIsolationSegmentFromOrganization(isolationSegmentGUID string, orgGUID string) (Warnings, error) {
   100  	request, err := client.newHTTPRequest(requestOptions{
   101  		RequestName: internal.DeleteIsolationSegmentRelationshipOrganizationRequest,
   102  		URIParams:   internal.Params{"isolation_segment_guid": isolationSegmentGUID, "organization_guid": orgGUID},
   103  	})
   104  	if err != nil {
   105  		return nil, err
   106  	}
   107  
   108  	var response cloudcontroller.Response
   109  	err = client.connection.Make(request, &response)
   110  
   111  	return response.Warnings, err
   112  }
   113  
   114  // GetOrganizationDefaultIsolationSegment returns the relationship between an
   115  // organization and it's default isolation segment.
   116  func (client *Client) GetOrganizationDefaultIsolationSegment(orgGUID string) (Relationship, Warnings, error) {
   117  	request, err := client.newHTTPRequest(requestOptions{
   118  		RequestName: internal.GetOrganizationDefaultIsolationSegmentRequest,
   119  		URIParams:   internal.Params{"organization_guid": orgGUID},
   120  	})
   121  	if err != nil {
   122  		return Relationship{}, nil, err
   123  	}
   124  
   125  	var relationship Relationship
   126  	response := cloudcontroller.Response{
   127  		Result: &relationship,
   128  	}
   129  
   130  	err = client.connection.Make(request, &response)
   131  	return relationship, response.Warnings, err
   132  }
   133  
   134  // PatchOrganizationDefaultIsolationSegment sets the default isolation segment
   135  // for an organization on the controller.
   136  // If isoSegGuid is empty it will reset the default isolation segment.
   137  func (client *Client) PatchOrganizationDefaultIsolationSegment(orgGUID string, isoSegGUID string) (Relationship, Warnings, error) {
   138  	body, err := json.Marshal(Relationship{GUID: isoSegGUID})
   139  	if err != nil {
   140  		return Relationship{}, nil, err
   141  	}
   142  
   143  	request, err := client.newHTTPRequest(requestOptions{
   144  		RequestName: internal.PatchOrganizationDefaultIsolationSegmentRequest,
   145  		Body:        bytes.NewReader(body),
   146  		URIParams:   internal.Params{"organization_guid": orgGUID},
   147  	})
   148  	if err != nil {
   149  		return Relationship{}, nil, err
   150  	}
   151  
   152  	var relationship Relationship
   153  	response := cloudcontroller.Response{
   154  		Result: &relationship,
   155  	}
   156  	err = client.connection.Make(request, &response)
   157  	return relationship, response.Warnings, err
   158  }
   159  
   160  // DeleteServiceInstanceRelationshipsSharedSpace will delete the sharing relationship
   161  // between the service instance and the shared-to space provided.
   162  func (client *Client) DeleteServiceInstanceRelationshipsSharedSpace(serviceInstanceGUID string, spaceGUID string) (Warnings, error) {
   163  	request, err := client.newHTTPRequest(requestOptions{
   164  		RequestName: internal.DeleteServiceInstanceRelationshipsSharedSpaceRequest,
   165  		URIParams:   internal.Params{"service_instance_guid": serviceInstanceGUID, "space_guid": spaceGUID},
   166  	})
   167  	if err != nil {
   168  		return nil, err
   169  	}
   170  
   171  	response := cloudcontroller.Response{}
   172  	err = client.connection.Make(request, &response)
   173  	return response.Warnings, err
   174  }