github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/api/cloudcontroller/ccv2/domain.go (about)

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