github.com/franc20/ayesa_sap@v7.0.0-beta.28.0.20200124003224-302d4d52fa6c+incompatible/api/cloudcontroller/ccv2/domain.go (about) 1 package ccv2 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/ccv2/constant" 10 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/internal" 11 ) 12 13 // Domain represents a Cloud Controller Domain. 14 type Domain struct { 15 // GUID is the unique domain identifier. 16 GUID string 17 18 // Internal indicates whether the domain is an internal domain. 19 Internal bool 20 21 // Name is the name given to the domain. 22 Name string 23 24 // RouterGroupGUID is the unique identier of the router group this domain is 25 // assigned to. 26 RouterGroupGUID string 27 28 // RouterGroupType is the type of router group this domain is assigned to. It 29 // can be of type `tcp` or `http`. 30 RouterGroupType constant.RouterGroupType 31 32 // DomainType is the access type of the domain. It can be either a domain 33 // private to a single org or it can be a domain shared to all orgs. 34 Type constant.DomainType 35 } 36 37 // UnmarshalJSON helps unmarshal a Cloud Controller Domain response. 38 func (domain *Domain) UnmarshalJSON(data []byte) error { 39 var ccDomain struct { 40 Metadata internal.Metadata `json:"metadata"` 41 Entity struct { 42 Name string `json:"name"` 43 RouterGroupGUID string `json:"router_group_guid"` 44 RouterGroupType string `json:"router_group_type"` 45 Internal bool `json:"internal"` 46 } `json:"entity"` 47 } 48 err := cloudcontroller.DecodeJSON(data, &ccDomain) 49 if err != nil { 50 return err 51 } 52 53 domain.GUID = ccDomain.Metadata.GUID 54 domain.Name = ccDomain.Entity.Name 55 domain.RouterGroupGUID = ccDomain.Entity.RouterGroupGUID 56 domain.RouterGroupType = constant.RouterGroupType(ccDomain.Entity.RouterGroupType) 57 domain.Internal = ccDomain.Entity.Internal 58 return nil 59 } 60 61 type createSharedDomainBody struct { 62 Name string `json:"name"` 63 RouterGroupGUID string `json:"router_group_guid,omitempty"` 64 Internal bool `json:"internal"` 65 } 66 67 func (client *Client) CreateSharedDomain(domainName string, routerGroupdGUID string, isInternal bool) (Warnings, error) { 68 body := createSharedDomainBody{ 69 Name: domainName, 70 RouterGroupGUID: routerGroupdGUID, 71 Internal: isInternal, 72 } 73 bodyBytes, err := json.Marshal(body) 74 request, err := client.newHTTPRequest(requestOptions{ 75 RequestName: internal.PostSharedDomainRequest, 76 Body: bytes.NewReader(bodyBytes), 77 }) 78 79 if err != nil { 80 return nil, err 81 } 82 83 var response cloudcontroller.Response 84 85 err = client.connection.Make(request, &response) 86 return response.Warnings, err 87 } 88 89 // GetOrganizationPrivateDomains returns the private domains associated with an organization. 90 func (client *Client) GetOrganizationPrivateDomains(orgGUID string, filters ...Filter) ([]Domain, Warnings, error) { 91 request, err := client.newHTTPRequest(requestOptions{ 92 RequestName: internal.GetOrganizationPrivateDomainsRequest, 93 Query: ConvertFilterParameters(filters), 94 URIParams: map[string]string{"organization_guid": orgGUID}, 95 }) 96 if err != nil { 97 return []Domain{}, nil, err 98 } 99 100 fullDomainsList := []Domain{} 101 warnings, err := client.paginate(request, Domain{}, func(item interface{}) error { 102 if domain, ok := item.(Domain); ok { 103 domain.Type = constant.PrivateDomain 104 fullDomainsList = append(fullDomainsList, domain) 105 } else { 106 return ccerror.UnknownObjectInListError{ 107 Expected: Domain{}, 108 Unexpected: item, 109 } 110 } 111 return nil 112 }) 113 114 return fullDomainsList, warnings, err 115 } 116 117 // GetPrivateDomain returns the Private Domain associated with the provided 118 // Domain GUID. 119 func (client *Client) GetPrivateDomain(domainGUID string) (Domain, Warnings, error) { 120 request, err := client.newHTTPRequest(requestOptions{ 121 RequestName: internal.GetPrivateDomainRequest, 122 URIParams: map[string]string{"private_domain_guid": domainGUID}, 123 }) 124 if err != nil { 125 return Domain{}, nil, err 126 } 127 128 var domain Domain 129 response := cloudcontroller.Response{ 130 DecodeJSONResponseInto: &domain, 131 } 132 133 err = client.connection.Make(request, &response) 134 if err != nil { 135 return Domain{}, response.Warnings, err 136 } 137 138 domain.Type = constant.PrivateDomain 139 return domain, response.Warnings, nil 140 } 141 142 // GetPrivateDomains returns the private domains this client has access to. 143 func (client *Client) GetPrivateDomains(filters ...Filter) ([]Domain, Warnings, error) { 144 request, err := client.newHTTPRequest(requestOptions{ 145 RequestName: internal.GetPrivateDomainsRequest, 146 Query: ConvertFilterParameters(filters), 147 }) 148 if err != nil { 149 return []Domain{}, nil, err 150 } 151 152 fullDomainsList := []Domain{} 153 warnings, err := client.paginate(request, Domain{}, func(item interface{}) error { 154 if domain, ok := item.(Domain); ok { 155 domain.Type = constant.PrivateDomain 156 fullDomainsList = append(fullDomainsList, domain) 157 } else { 158 return ccerror.UnknownObjectInListError{ 159 Expected: Domain{}, 160 Unexpected: item, 161 } 162 } 163 return nil 164 }) 165 166 return fullDomainsList, warnings, err 167 } 168 169 // GetSharedDomain returns the Shared Domain associated with the provided 170 // Domain GUID. 171 func (client *Client) GetSharedDomain(domainGUID string) (Domain, Warnings, error) { 172 request, err := client.newHTTPRequest(requestOptions{ 173 RequestName: internal.GetSharedDomainRequest, 174 URIParams: map[string]string{"shared_domain_guid": domainGUID}, 175 }) 176 if err != nil { 177 return Domain{}, nil, err 178 } 179 180 var domain Domain 181 response := cloudcontroller.Response{ 182 DecodeJSONResponseInto: &domain, 183 } 184 185 err = client.connection.Make(request, &response) 186 if err != nil { 187 return Domain{}, response.Warnings, err 188 } 189 190 domain.Type = constant.SharedDomain 191 return domain, response.Warnings, nil 192 } 193 194 // GetSharedDomains returns the global shared domains. 195 func (client *Client) GetSharedDomains(filters ...Filter) ([]Domain, Warnings, error) { 196 request, err := client.newHTTPRequest(requestOptions{ 197 RequestName: internal.GetSharedDomainsRequest, 198 Query: ConvertFilterParameters(filters), 199 }) 200 if err != nil { 201 return []Domain{}, nil, err 202 } 203 204 fullDomainsList := []Domain{} 205 warnings, err := client.paginate(request, Domain{}, func(item interface{}) error { 206 if domain, ok := item.(Domain); ok { 207 domain.Type = constant.SharedDomain 208 fullDomainsList = append(fullDomainsList, domain) 209 } else { 210 return ccerror.UnknownObjectInListError{ 211 Expected: Domain{}, 212 Unexpected: item, 213 } 214 } 215 return nil 216 }) 217 218 return fullDomainsList, warnings, err 219 }