github.com/jghiloni/cli@v6.28.1-0.20170628223758-0ce05fe032a2+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 var ccRelationship struct { 19 Data struct { 20 GUID *string `json:"guid"` 21 } `json:"data"` 22 } 23 24 if r.GUID != "" { 25 ccRelationship.Data.GUID = &r.GUID 26 } 27 return json.Marshal(ccRelationship) 28 } 29 30 func (r *Relationship) UnmarshalJSON(data []byte) error { 31 var ccRelationship struct { 32 Data struct { 33 GUID string `json:"guid"` 34 } `json:"data"` 35 } 36 37 err := json.Unmarshal(data, &ccRelationship) 38 if err != nil { 39 return err 40 } 41 42 r.GUID = ccRelationship.Data.GUID 43 return nil 44 } 45 46 // AssignSpaceToIsolationSegment assigns an isolation segment to a space and 47 // returns the relationship. 48 func (client *Client) AssignSpaceToIsolationSegment(spaceGUID string, isolationSegmentGUID string) (Relationship, Warnings, error) { 49 body, err := json.Marshal(Relationship{GUID: isolationSegmentGUID}) 50 if err != nil { 51 return Relationship{}, nil, err 52 } 53 54 request, err := client.newHTTPRequest(requestOptions{ 55 RequestName: internal.PatchSpaceRelationshipIsolationSegmentRequest, 56 URIParams: internal.Params{"guid": spaceGUID}, 57 Body: bytes.NewReader(body), 58 }) 59 60 var relationship Relationship 61 response := cloudcontroller.Response{ 62 Result: &relationship, 63 } 64 65 err = client.connection.Make(request, &response) 66 return relationship, response.Warnings, err 67 } 68 69 // GetSpaceIsolationSegment returns the relationship between a space and it's 70 // isolation segment. 71 func (client *Client) GetSpaceIsolationSegment(spaceGUID string) (Relationship, Warnings, error) { 72 request, err := client.newHTTPRequest(requestOptions{ 73 RequestName: internal.GetSpaceRelationshipIsolationSegmentRequest, 74 URIParams: internal.Params{"guid": spaceGUID}, 75 }) 76 if err != nil { 77 return Relationship{}, nil, err 78 } 79 80 var relationship Relationship 81 response := cloudcontroller.Response{ 82 Result: &relationship, 83 } 84 85 err = client.connection.Make(request, &response) 86 return relationship, response.Warnings, err 87 } 88 89 // RevokeIsolationSegmentFromOrganization will delete the relationship between 90 // the isolation segment and the organization provided. 91 func (client *Client) RevokeIsolationSegmentFromOrganization(isolationSegmentGUID string, organizationGUID string) (Warnings, error) { 92 request, err := client.newHTTPRequest(requestOptions{ 93 RequestName: internal.DeleteIsolationSegmentRelationshipOrganizationRequest, 94 URIParams: internal.Params{"guid": isolationSegmentGUID, "org_guid": organizationGUID}, 95 }) 96 if err != nil { 97 return nil, err 98 } 99 100 var response cloudcontroller.Response 101 err = client.connection.Make(request, &response) 102 103 return response.Warnings, err 104 } 105 106 // GetOrganizationDefaultIsolationSegment returns the relationship between an 107 // organization and it's default isolation segment. 108 func (client *Client) GetOrganizationDefaultIsolationSegment(orgGUID string) (Relationship, Warnings, error) { 109 request, err := client.newHTTPRequest(requestOptions{ 110 RequestName: internal.GetOrganizationDefaultIsolationSegmentRequest, 111 URIParams: internal.Params{"guid": orgGUID}, 112 }) 113 if err != nil { 114 return Relationship{}, nil, err 115 } 116 117 var relationship Relationship 118 response := cloudcontroller.Response{ 119 Result: &relationship, 120 } 121 122 err = client.connection.Make(request, &response) 123 return relationship, response.Warnings, err 124 }