github.com/orange-cloudfoundry/cli@v7.1.0+incompatible/api/cloudcontroller/ccv2/domain.go (about)

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