github.com/arunkumar7540/cli@v6.45.0+incompatible/api/cloudcontroller/ccv3/organization.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/ccerror" 9 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/internal" 10 ) 11 12 // Organization represents a Cloud Controller V3 Organization. 13 type Organization struct { 14 // GUID is the unique organization identifier. 15 GUID string `json:"guid,omitempty"` 16 // Name is the name of the organization. 17 Name string `json:"name"` 18 19 // Metadata is used for custom tagging of API resources 20 Metadata *Metadata `json:"metadata,omitempty"` 21 } 22 23 func (client *Client) GetDefaultDomain(orgGUID string) (Domain, Warnings, error) { 24 request, err := client.newHTTPRequest(requestOptions{ 25 RequestName: internal.GetDefaultDomainRequest, 26 URIParams: map[string]string{"organization_guid": orgGUID}, 27 }) 28 if err != nil { 29 return Domain{}, nil, err 30 } 31 32 var defaultDomain Domain 33 34 response := cloudcontroller.Response{ 35 DecodeJSONResponseInto: &defaultDomain, 36 } 37 38 err = client.connection.Make(request, &response) 39 40 return defaultDomain, response.Warnings, err 41 } 42 43 // GetIsolationSegmentOrganizations lists organizations 44 // entitled to an isolation segment. 45 func (client *Client) GetIsolationSegmentOrganizations(isolationSegmentGUID string) ([]Organization, Warnings, error) { 46 request, err := client.newHTTPRequest(requestOptions{ 47 RequestName: internal.GetIsolationSegmentOrganizationsRequest, 48 URIParams: map[string]string{"isolation_segment_guid": isolationSegmentGUID}, 49 }) 50 if err != nil { 51 return nil, nil, err 52 } 53 54 var fullOrgsList []Organization 55 warnings, err := client.paginate(request, Organization{}, func(item interface{}) error { 56 if app, ok := item.(Organization); ok { 57 fullOrgsList = append(fullOrgsList, app) 58 } else { 59 return ccerror.UnknownObjectInListError{ 60 Expected: Organization{}, 61 Unexpected: item, 62 } 63 } 64 return nil 65 }) 66 67 return fullOrgsList, warnings, err 68 } 69 70 // GetOrganizations lists organizations with optional filters. 71 func (client *Client) GetOrganizations(query ...Query) ([]Organization, Warnings, error) { 72 request, err := client.newHTTPRequest(requestOptions{ 73 RequestName: internal.GetOrganizationsRequest, 74 Query: query, 75 }) 76 if err != nil { 77 return nil, nil, err 78 } 79 80 var fullOrgsList []Organization 81 warnings, err := client.paginate(request, Organization{}, func(item interface{}) error { 82 if app, ok := item.(Organization); ok { 83 fullOrgsList = append(fullOrgsList, app) 84 } else { 85 return ccerror.UnknownObjectInListError{ 86 Expected: Organization{}, 87 Unexpected: item, 88 } 89 } 90 return nil 91 }) 92 93 return fullOrgsList, warnings, err 94 } 95 96 func (client *Client) UpdateOrganization(org Organization) (Organization, Warnings, error) { 97 orgGUID := org.GUID 98 org.GUID = "" 99 orgBytes, err := json.Marshal(org) 100 if err != nil { 101 return Organization{}, nil, err 102 } 103 request, err := client.newHTTPRequest(requestOptions{ 104 RequestName: internal.PatchOrganizationRequest, 105 Body: bytes.NewReader(orgBytes), 106 URIParams: map[string]string{"organization_guid": orgGUID}, 107 }) 108 109 if err != nil { 110 return Organization{}, nil, err 111 } 112 113 var responseOrg Organization 114 response := cloudcontroller.Response{ 115 DecodeJSONResponseInto: &responseOrg, 116 } 117 err = client.connection.Make(request, &response) 118 119 if err != nil { 120 return Organization{}, nil, err 121 } 122 return responseOrg, nil, err 123 }