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