github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/api/cloudcontroller/ccv3/domain.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  	"code.cloudfoundry.org/cli/types"
    11  )
    12  
    13  type Domain struct {
    14  	GUID             string         `json:"guid,omitempty"`
    15  	Name             string         `json:"name"`
    16  	Internal         types.NullBool `json:"internal,omitempty"`
    17  	OrganizationGUID string         `json:"orgguid,omitempty"`
    18  
    19  	// Metadata is used for custom tagging of API resources
    20  	Metadata *Metadata `json:"metadata,omitempty"`
    21  }
    22  
    23  func (d Domain) MarshalJSON() ([]byte, error) {
    24  	type Data struct {
    25  		GUID string `json:"guid,omitempty"`
    26  	}
    27  
    28  	type OrgData struct {
    29  		Data Data `json:"data,omitempty"`
    30  	}
    31  
    32  	type OrgRelationship struct {
    33  		Org OrgData `json:"organization,omitempty"`
    34  	}
    35  
    36  	type ccDomain struct {
    37  		GUID          string           `json:"guid,omitempty"`
    38  		Name          string           `json:"name"`
    39  		Internal      *bool            `json:"internal,omitempty"`
    40  		Relationships *OrgRelationship `json:"relationships,omitempty"`
    41  	}
    42  
    43  	ccDom := ccDomain{
    44  		Name: d.Name,
    45  	}
    46  
    47  	if d.Internal.IsSet {
    48  		ccDom.Internal = &d.Internal.Value
    49  	}
    50  
    51  	if d.GUID != "" {
    52  		ccDom.GUID = d.GUID
    53  	}
    54  
    55  	if d.OrganizationGUID != "" {
    56  		ccDom.Relationships = &OrgRelationship{OrgData{Data{GUID: d.OrganizationGUID}}}
    57  	}
    58  	return json.Marshal(ccDom)
    59  }
    60  
    61  func (d *Domain) UnmarshalJSON(data []byte) error {
    62  	var ccRouteStruct struct {
    63  		GUID          string         `json:"guid,omitempty"`
    64  		Name          string         `json:"name"`
    65  		Internal      types.NullBool `json:"internal,omitempty"`
    66  		Relationships struct {
    67  			Organization struct {
    68  				Data struct {
    69  					GUID string `json:"guid,omitempty"`
    70  				} `json:"data,omitempty"`
    71  			} `json:"organization,omitempty"`
    72  		} `json:"relationships,omitempty"`
    73  		Metadata *Metadata
    74  	}
    75  
    76  	err := cloudcontroller.DecodeJSON(data, &ccRouteStruct)
    77  	if err != nil {
    78  		return err
    79  	}
    80  
    81  	d.GUID = ccRouteStruct.GUID
    82  	d.Name = ccRouteStruct.Name
    83  	d.Internal = ccRouteStruct.Internal
    84  	d.OrganizationGUID = ccRouteStruct.Relationships.Organization.Data.GUID
    85  	d.Metadata = ccRouteStruct.Metadata
    86  	return nil
    87  }
    88  
    89  type SharedOrgs struct {
    90  	GUIDs []string
    91  }
    92  
    93  func (sharedOrgs SharedOrgs) MarshalJSON() ([]byte, error) {
    94  	type Org struct {
    95  		GUID string `json:"guid,omitempty"`
    96  	}
    97  
    98  	type Data = []Org
    99  
   100  	type sharedOrgsRelationship struct {
   101  		Data Data `json:"data"`
   102  	}
   103  
   104  	var orgs []Org
   105  	for _, sharedOrgGUID := range sharedOrgs.GUIDs {
   106  		orgs = append(orgs, Org{GUID: sharedOrgGUID})
   107  	}
   108  
   109  	relationship := sharedOrgsRelationship{
   110  		Data: orgs,
   111  	}
   112  
   113  	return json.Marshal(relationship)
   114  }
   115  
   116  func (sharedOrgs *SharedOrgs) UnmarshalJSON(data []byte) error {
   117  	var alias struct {
   118  		Data []struct {
   119  			GUID string `json:"guid,omitempty"`
   120  		} `json:"data,omitempty"`
   121  	}
   122  
   123  	err := cloudcontroller.DecodeJSON(data, &alias)
   124  	if err != nil {
   125  		return err
   126  	}
   127  
   128  	var guids []string
   129  	for _, org := range alias.Data {
   130  		guids = append(guids, org.GUID)
   131  	}
   132  
   133  	sharedOrgs.GUIDs = guids
   134  	return nil
   135  }
   136  
   137  // CheckRoute checks whether the route with the given domain GUID, hostname,
   138  // and path exists in the foundation.
   139  func (client Client) CheckRoute(domainGUID string, hostname string, path string) (bool, Warnings, error) {
   140  	var query []Query
   141  
   142  	if hostname != "" {
   143  		query = append(query, Query{Key: HostFilter, Values: []string{hostname}})
   144  	}
   145  
   146  	if path != "" {
   147  		query = append(query, Query{Key: PathFilter, Values: []string{path}})
   148  	}
   149  
   150  	request, err := client.newHTTPRequest(requestOptions{
   151  		RequestName: internal.GetDomainRouteReservationsRequest,
   152  		URIParams:   map[string]string{"domain_guid": domainGUID},
   153  		Query:       query,
   154  	})
   155  	if err != nil {
   156  		return false, nil, err
   157  	}
   158  
   159  	var responseJson struct {
   160  		MatchingRoute bool `json:"matching_route"`
   161  	}
   162  
   163  	response := cloudcontroller.Response{
   164  		DecodeJSONResponseInto: &responseJson,
   165  	}
   166  	err = client.connection.Make(request, &response)
   167  
   168  	return responseJson.MatchingRoute, response.Warnings, err
   169  }
   170  
   171  func (client Client) CreateDomain(domain Domain) (Domain, Warnings, error) {
   172  	bodyBytes, err := json.Marshal(domain)
   173  	if err != nil {
   174  		return Domain{}, nil, err
   175  	}
   176  
   177  	request, err := client.newHTTPRequest(requestOptions{
   178  		RequestName: internal.PostDomainRequest,
   179  		Body:        bytes.NewReader(bodyBytes),
   180  	})
   181  
   182  	if err != nil {
   183  		return Domain{}, nil, err
   184  	}
   185  
   186  	var ccDomain Domain
   187  	response := cloudcontroller.Response{
   188  		DecodeJSONResponseInto: &ccDomain,
   189  	}
   190  
   191  	err = client.connection.Make(request, &response)
   192  
   193  	return ccDomain, response.Warnings, err
   194  }
   195  
   196  func (client Client) DeleteDomain(domainGUID string) (JobURL, Warnings, error) {
   197  	request, err := client.newHTTPRequest(requestOptions{
   198  		URIParams: map[string]string{
   199  			"domain_guid": domainGUID,
   200  		},
   201  		RequestName: internal.DeleteDomainRequest,
   202  	})
   203  	if err != nil {
   204  		return "", nil, err
   205  	}
   206  
   207  	response := cloudcontroller.Response{}
   208  	err = client.connection.Make(request, &response)
   209  
   210  	return JobURL(response.ResourceLocationURL), response.Warnings, err
   211  }
   212  
   213  // GetDomain returns a domain with the given GUID.
   214  func (client *Client) GetDomain(domainGUID string) (Domain, Warnings, error) {
   215  	request, err := client.newHTTPRequest(requestOptions{
   216  		RequestName: internal.GetDomainRequest,
   217  		URIParams:   map[string]string{"domain_guid": domainGUID},
   218  	})
   219  	if err != nil {
   220  		return Domain{}, nil, err
   221  	}
   222  
   223  	var responseDomain Domain
   224  	response := cloudcontroller.Response{
   225  		DecodeJSONResponseInto: &responseDomain,
   226  	}
   227  	err = client.connection.Make(request, &response)
   228  
   229  	return responseDomain, response.Warnings, err
   230  }
   231  
   232  func (client Client) GetDomains(query ...Query) ([]Domain, Warnings, error) {
   233  	request, err := client.newHTTPRequest(requestOptions{
   234  		RequestName: internal.GetDomainsRequest,
   235  		Query:       query,
   236  	})
   237  	if err != nil {
   238  		return nil, nil, err
   239  	}
   240  
   241  	var fullDomainsList []Domain
   242  	warnings, err := client.paginate(request, Domain{}, func(item interface{}) error {
   243  		if domain, ok := item.(Domain); ok {
   244  			fullDomainsList = append(fullDomainsList, domain)
   245  		} else {
   246  			return ccerror.UnknownObjectInListError{
   247  				Expected:   Domain{},
   248  				Unexpected: item,
   249  			}
   250  		}
   251  		return nil
   252  	})
   253  
   254  	return fullDomainsList, warnings, err
   255  }
   256  
   257  func (client Client) GetOrganizationDomains(orgGUID string, query ...Query) ([]Domain, Warnings, error) {
   258  	request, err := client.newHTTPRequest(requestOptions{
   259  		URIParams:   internal.Params{"organization_guid": orgGUID},
   260  		RequestName: internal.GetOrganizationDomainsRequest,
   261  		Query:       query,
   262  	})
   263  	if err != nil {
   264  		return nil, nil, err
   265  	}
   266  
   267  	var fullDomainsList []Domain
   268  	warnings, err := client.paginate(request, Domain{}, func(item interface{}) error {
   269  		if domain, ok := item.(Domain); ok {
   270  			fullDomainsList = append(fullDomainsList, domain)
   271  		} else {
   272  			return ccerror.UnknownObjectInListError{
   273  				Expected:   Domain{},
   274  				Unexpected: item,
   275  			}
   276  		}
   277  		return nil
   278  	})
   279  
   280  	return fullDomainsList, warnings, err
   281  }
   282  
   283  func (client Client) SharePrivateDomainToOrgs(domainGuid string, sharedOrgs SharedOrgs) (Warnings, error) {
   284  	bodyBytes, err := json.Marshal(sharedOrgs)
   285  
   286  	if err != nil {
   287  		return nil, err
   288  	}
   289  
   290  	request, err := client.newHTTPRequest(requestOptions{
   291  		URIParams:   internal.Params{"domain_guid": domainGuid},
   292  		RequestName: internal.SharePrivateDomainRequest,
   293  		Body:        bytes.NewReader(bodyBytes),
   294  	})
   295  
   296  	if err != nil {
   297  		return nil, err
   298  	}
   299  
   300  	var ccSharedOrgs SharedOrgs
   301  	response := cloudcontroller.Response{
   302  		DecodeJSONResponseInto: &ccSharedOrgs,
   303  	}
   304  
   305  	err = client.connection.Make(request, &response)
   306  
   307  	return response.Warnings, err
   308  }
   309  
   310  func (client Client) UnsharePrivateDomainFromOrg(domainGuid string, orgGUID string) (Warnings, error) {
   311  	request, err := client.newHTTPRequest(requestOptions{
   312  		URIParams:   internal.Params{"domain_guid": domainGuid, "org_guid": orgGUID},
   313  		RequestName: internal.DeleteSharedOrgFromDomainRequest,
   314  	})
   315  
   316  	if err != nil {
   317  		return nil, err
   318  	}
   319  
   320  	var response cloudcontroller.Response
   321  
   322  	err = client.connection.Make(request, &response)
   323  	return response.Warnings, err
   324  }