github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/api/cloudcontroller/ccv3/domain.go (about)

     1  package ccv3
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  
     7  	"code.cloudfoundry.org/cli/api/cloudcontroller"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/internal"
     9  	"code.cloudfoundry.org/cli/resources"
    10  )
    11  
    12  type SharedOrgs struct {
    13  	GUIDs []string
    14  }
    15  
    16  func (sharedOrgs SharedOrgs) MarshalJSON() ([]byte, error) {
    17  	type Org struct {
    18  		GUID string `json:"guid,omitempty"`
    19  	}
    20  
    21  	type Data = []Org
    22  
    23  	type sharedOrgsRelationship struct {
    24  		Data Data `json:"data"`
    25  	}
    26  
    27  	var orgs []Org
    28  	for _, sharedOrgGUID := range sharedOrgs.GUIDs {
    29  		orgs = append(orgs, Org{GUID: sharedOrgGUID})
    30  	}
    31  
    32  	relationship := sharedOrgsRelationship{
    33  		Data: orgs,
    34  	}
    35  
    36  	return json.Marshal(relationship)
    37  }
    38  
    39  func (sharedOrgs *SharedOrgs) UnmarshalJSON(data []byte) error {
    40  	var alias struct {
    41  		Data []struct {
    42  			GUID string `json:"guid,omitempty"`
    43  		} `json:"data,omitempty"`
    44  	}
    45  
    46  	err := cloudcontroller.DecodeJSON(data, &alias)
    47  	if err != nil {
    48  		return err
    49  	}
    50  
    51  	var guids []string
    52  	for _, org := range alias.Data {
    53  		guids = append(guids, org.GUID)
    54  	}
    55  
    56  	sharedOrgs.GUIDs = guids
    57  	return nil
    58  }
    59  
    60  // CheckRoute checks whether the route with the given domain GUID, hostname,
    61  // and path exists in the foundation.
    62  func (client Client) CheckRoute(domainGUID string, hostname string, path string, port int) (bool, Warnings, error) {
    63  	var query []Query
    64  
    65  	if hostname != "" {
    66  		query = append(query, Query{Key: HostFilter, Values: []string{hostname}})
    67  	}
    68  
    69  	if path != "" {
    70  		query = append(query, Query{Key: PathFilter, Values: []string{path}})
    71  	}
    72  
    73  	if port != 0 {
    74  		query = append(query, Query{Key: PortFilter, Values: []string{fmt.Sprintf("%d", port)}})
    75  	}
    76  
    77  	var responseBody struct {
    78  		MatchingRoute bool `json:"matching_route"`
    79  	}
    80  
    81  	_, warnings, err := client.MakeRequest(RequestParams{
    82  		RequestName:  internal.GetDomainRouteReservationsRequest,
    83  		URIParams:    internal.Params{"domain_guid": domainGUID},
    84  		Query:        query,
    85  		ResponseBody: &responseBody,
    86  	})
    87  
    88  	return responseBody.MatchingRoute, warnings, err
    89  }
    90  
    91  func (client Client) CreateDomain(domain resources.Domain) (resources.Domain, Warnings, error) {
    92  	var responseBody resources.Domain
    93  
    94  	_, warnings, err := client.MakeRequest(RequestParams{
    95  		RequestName:  internal.PostDomainRequest,
    96  		RequestBody:  domain,
    97  		ResponseBody: &responseBody,
    98  	})
    99  
   100  	return responseBody, warnings, err
   101  }
   102  
   103  func (client Client) DeleteDomain(domainGUID string) (JobURL, Warnings, error) {
   104  	jobURL, warnings, err := client.MakeRequest(RequestParams{
   105  		RequestName: internal.DeleteDomainRequest,
   106  		URIParams:   internal.Params{"domain_guid": domainGUID},
   107  	})
   108  
   109  	return jobURL, warnings, err
   110  }
   111  
   112  // GetDomain returns a domain with the given GUID.
   113  func (client *Client) GetDomain(domainGUID string) (resources.Domain, Warnings, error) {
   114  	var responseBody resources.Domain
   115  
   116  	_, warnings, err := client.MakeRequest(RequestParams{
   117  		RequestName:  internal.GetDomainRequest,
   118  		URIParams:    internal.Params{"domain_guid": domainGUID},
   119  		ResponseBody: &responseBody,
   120  	})
   121  
   122  	return responseBody, warnings, err
   123  }
   124  
   125  func (client Client) GetDomains(query ...Query) ([]resources.Domain, Warnings, error) {
   126  	var domains []resources.Domain
   127  
   128  	_, warnings, err := client.MakeListRequest(RequestParams{
   129  		RequestName:  internal.GetDomainsRequest,
   130  		Query:        query,
   131  		ResponseBody: resources.Domain{},
   132  		AppendToList: func(item interface{}) error {
   133  			domains = append(domains, item.(resources.Domain))
   134  			return nil
   135  		},
   136  	})
   137  
   138  	return domains, warnings, err
   139  }
   140  
   141  func (client Client) GetOrganizationDomains(orgGUID string, query ...Query) ([]resources.Domain, Warnings, error) {
   142  	var domains []resources.Domain
   143  
   144  	_, warnings, err := client.MakeListRequest(RequestParams{
   145  		URIParams:    internal.Params{"organization_guid": orgGUID},
   146  		RequestName:  internal.GetOrganizationDomainsRequest,
   147  		Query:        query,
   148  		ResponseBody: resources.Domain{},
   149  		AppendToList: func(item interface{}) error {
   150  			domains = append(domains, item.(resources.Domain))
   151  			return nil
   152  		},
   153  	})
   154  
   155  	return domains, warnings, err
   156  }
   157  
   158  func (client Client) SharePrivateDomainToOrgs(domainGuid string, sharedOrgs SharedOrgs) (Warnings, error) {
   159  	_, warnings, err := client.MakeRequest(RequestParams{
   160  		RequestName: internal.SharePrivateDomainRequest,
   161  		URIParams:   internal.Params{"domain_guid": domainGuid},
   162  		RequestBody: sharedOrgs,
   163  	})
   164  
   165  	return warnings, err
   166  }
   167  
   168  func (client Client) UnsharePrivateDomainFromOrg(domainGuid string, orgGUID string) (Warnings, error) {
   169  	_, warnings, err := client.MakeRequest(RequestParams{
   170  		RequestName: internal.DeleteSharedOrgFromDomainRequest,
   171  		URIParams:   internal.Params{"domain_guid": domainGuid, "org_guid": orgGUID},
   172  	})
   173  
   174  	return warnings, err
   175  }