github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+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 := cloudcontroller.DecodeJSON(data, &ccRelationship)
    43  	if err != nil {
    44  		return err
    45  	}
    46  
    47  	r.GUID = ccRelationship.Data.GUID
    48  	return nil
    49  }
    50  
    51  // DeleteIsolationSegmentOrganization will delete the relationship between
    52  // the isolation segment and the organization provided.
    53  func (client *Client) DeleteIsolationSegmentOrganization(isolationSegmentGUID string, orgGUID string) (Warnings, error) {
    54  	request, err := client.newHTTPRequest(requestOptions{
    55  		RequestName: internal.DeleteIsolationSegmentRelationshipOrganizationRequest,
    56  		URIParams:   internal.Params{"isolation_segment_guid": isolationSegmentGUID, "organization_guid": orgGUID},
    57  	})
    58  	if err != nil {
    59  		return nil, err
    60  	}
    61  
    62  	var response cloudcontroller.Response
    63  	err = client.connection.Make(request, &response)
    64  
    65  	return response.Warnings, err
    66  }
    67  
    68  // DeleteServiceInstanceRelationshipsSharedSpace will delete the sharing relationship
    69  // between the service instance and the shared-to space provided.
    70  func (client *Client) DeleteServiceInstanceRelationshipsSharedSpace(serviceInstanceGUID string, spaceGUID string) (Warnings, error) {
    71  	request, err := client.newHTTPRequest(requestOptions{
    72  		RequestName: internal.DeleteServiceInstanceRelationshipsSharedSpaceRequest,
    73  		URIParams:   internal.Params{"service_instance_guid": serviceInstanceGUID, "space_guid": spaceGUID},
    74  	})
    75  	if err != nil {
    76  		return nil, err
    77  	}
    78  
    79  	response := cloudcontroller.Response{}
    80  	err = client.connection.Make(request, &response)
    81  	return response.Warnings, err
    82  }
    83  
    84  // GetOrganizationDefaultIsolationSegment returns the relationship between an
    85  // organization and it's default isolation segment.
    86  func (client *Client) GetOrganizationDefaultIsolationSegment(orgGUID string) (Relationship, Warnings, error) {
    87  	request, err := client.newHTTPRequest(requestOptions{
    88  		RequestName: internal.GetOrganizationRelationshipDefaultIsolationSegmentRequest,
    89  		URIParams:   internal.Params{"organization_guid": orgGUID},
    90  	})
    91  	if err != nil {
    92  		return Relationship{}, nil, err
    93  	}
    94  
    95  	var relationship Relationship
    96  	response := cloudcontroller.Response{
    97  		DecodeJSONResponseInto: &relationship,
    98  	}
    99  
   100  	err = client.connection.Make(request, &response)
   101  	return relationship, response.Warnings, err
   102  }
   103  
   104  // GetSpaceIsolationSegment returns the relationship between a space and it's
   105  // isolation segment.
   106  func (client *Client) GetSpaceIsolationSegment(spaceGUID string) (Relationship, Warnings, error) {
   107  	request, err := client.newHTTPRequest(requestOptions{
   108  		RequestName: internal.GetSpaceRelationshipIsolationSegmentRequest,
   109  		URIParams:   internal.Params{"space_guid": spaceGUID},
   110  	})
   111  	if err != nil {
   112  		return Relationship{}, nil, err
   113  	}
   114  
   115  	var relationship Relationship
   116  	response := cloudcontroller.Response{
   117  		DecodeJSONResponseInto: &relationship,
   118  	}
   119  
   120  	err = client.connection.Make(request, &response)
   121  	return relationship, response.Warnings, err
   122  }
   123  
   124  // SetApplicationDroplet sets the specified droplet on the given application.
   125  func (client *Client) SetApplicationDroplet(appGUID string, dropletGUID string) (Relationship, Warnings, error) {
   126  	relationship := Relationship{GUID: dropletGUID}
   127  	bodyBytes, err := json.Marshal(relationship)
   128  	if err != nil {
   129  		return Relationship{}, nil, err
   130  	}
   131  
   132  	request, err := client.newHTTPRequest(requestOptions{
   133  		RequestName: internal.PatchApplicationCurrentDropletRequest,
   134  		URIParams:   map[string]string{"app_guid": appGUID},
   135  		Body:        bytes.NewReader(bodyBytes),
   136  	})
   137  	if err != nil {
   138  		return Relationship{}, nil, err
   139  	}
   140  
   141  	var responseRelationship Relationship
   142  	response := cloudcontroller.Response{
   143  		DecodeJSONResponseInto: &responseRelationship,
   144  	}
   145  	err = client.connection.Make(request, &response)
   146  
   147  	return responseRelationship, response.Warnings, err
   148  }
   149  
   150  // UpdateOrganizationDefaultIsolationSegmentRelationship sets the default isolation segment
   151  // for an organization on the controller.
   152  // If isoSegGuid is empty it will reset the default isolation segment.
   153  func (client *Client) UpdateOrganizationDefaultIsolationSegmentRelationship(orgGUID string, isoSegGUID string) (Relationship, Warnings, error) {
   154  	body, err := json.Marshal(Relationship{GUID: isoSegGUID})
   155  	if err != nil {
   156  		return Relationship{}, nil, err
   157  	}
   158  
   159  	request, err := client.newHTTPRequest(requestOptions{
   160  		RequestName: internal.PatchOrganizationRelationshipDefaultIsolationSegmentRequest,
   161  		Body:        bytes.NewReader(body),
   162  		URIParams:   internal.Params{"organization_guid": orgGUID},
   163  	})
   164  	if err != nil {
   165  		return Relationship{}, nil, err
   166  	}
   167  
   168  	var relationship Relationship
   169  	response := cloudcontroller.Response{
   170  		DecodeJSONResponseInto: &relationship,
   171  	}
   172  	err = client.connection.Make(request, &response)
   173  	return relationship, response.Warnings, err
   174  }
   175  
   176  // UpdateSpaceIsolationSegmentRelationship assigns an isolation segment to a space and
   177  // returns the relationship.
   178  func (client *Client) UpdateSpaceIsolationSegmentRelationship(spaceGUID string, isolationSegmentGUID string) (Relationship, Warnings, error) {
   179  	body, err := json.Marshal(Relationship{GUID: isolationSegmentGUID})
   180  	if err != nil {
   181  		return Relationship{}, nil, err
   182  	}
   183  
   184  	request, err := client.newHTTPRequest(requestOptions{
   185  		RequestName: internal.PatchSpaceRelationshipIsolationSegmentRequest,
   186  		URIParams:   internal.Params{"space_guid": spaceGUID},
   187  		Body:        bytes.NewReader(body),
   188  	})
   189  	if err != nil {
   190  		return Relationship{}, nil, err
   191  	}
   192  
   193  	var relationship Relationship
   194  	response := cloudcontroller.Response{
   195  		DecodeJSONResponseInto: &relationship,
   196  	}
   197  
   198  	err = client.connection.Make(request, &response)
   199  	return relationship, response.Warnings, err
   200  }