github.com/willmadison/cli@v6.40.1-0.20181018160101-29d5937903ff+incompatible/api/cloudcontroller/ccv2/domain.go (about)

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