github.com/lukasheimann/cloudfoundrycli@v7.1.0+incompatible/actor/v7action/domain.go (about)

     1  package v7action
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/actor/actionerror"
     5  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
     6  	"code.cloudfoundry.org/cli/resources"
     7  	"code.cloudfoundry.org/cli/types"
     8  )
     9  
    10  type SharedOrgs ccv3.SharedOrgs
    11  
    12  func (actor Actor) CheckRoute(domainName string, hostname string, path string, port int) (bool, Warnings, error) {
    13  	var allWarnings Warnings
    14  
    15  	domain, warnings, err := actor.GetDomainByName(domainName)
    16  	allWarnings = append(allWarnings, warnings...)
    17  	if err != nil {
    18  		return false, allWarnings, err
    19  	}
    20  
    21  	matches, checkRouteWarnings, err := actor.CloudControllerClient.CheckRoute(domain.GUID, hostname, path, port)
    22  	allWarnings = append(allWarnings, checkRouteWarnings...)
    23  
    24  	return matches, allWarnings, err
    25  }
    26  
    27  func (actor Actor) CreateSharedDomain(domainName string, internal bool, routerGroupName string) (Warnings, error) {
    28  	allWarnings := Warnings{}
    29  	routerGroupGUID := ""
    30  
    31  	if routerGroupName != "" {
    32  		routerGroup, err := actor.GetRouterGroupByName(routerGroupName)
    33  		if err != nil {
    34  			return allWarnings, err
    35  		}
    36  
    37  		routerGroupGUID = routerGroup.GUID
    38  	}
    39  
    40  	_, warnings, err := actor.CloudControllerClient.CreateDomain(resources.Domain{
    41  		Name:        domainName,
    42  		Internal:    types.NullBool{IsSet: true, Value: internal},
    43  		RouterGroup: routerGroupGUID,
    44  	})
    45  	allWarnings = append(allWarnings, Warnings(warnings)...)
    46  
    47  	return allWarnings, err
    48  }
    49  
    50  func (actor Actor) CreatePrivateDomain(domainName string, orgName string) (Warnings, error) {
    51  	allWarnings := Warnings{}
    52  	organization, warnings, err := actor.GetOrganizationByName(orgName)
    53  	allWarnings = append(allWarnings, warnings...)
    54  
    55  	if err != nil {
    56  		return allWarnings, err
    57  	}
    58  	_, apiWarnings, err := actor.CloudControllerClient.CreateDomain(resources.Domain{
    59  		Name:             domainName,
    60  		OrganizationGUID: organization.GUID,
    61  	})
    62  
    63  	actorWarnings := Warnings(apiWarnings)
    64  	allWarnings = append(allWarnings, actorWarnings...)
    65  
    66  	return allWarnings, err
    67  }
    68  
    69  func (actor Actor) DeleteDomain(domain resources.Domain) (Warnings, error) {
    70  	allWarnings := Warnings{}
    71  
    72  	jobURL, apiWarnings, err := actor.CloudControllerClient.DeleteDomain(domain.GUID)
    73  
    74  	actorWarnings := Warnings(apiWarnings)
    75  	allWarnings = append(allWarnings, actorWarnings...)
    76  
    77  	if err != nil {
    78  		return allWarnings, err
    79  	}
    80  
    81  	pollJobWarnings, err := actor.CloudControllerClient.PollJob(jobURL)
    82  	allWarnings = append(allWarnings, Warnings(pollJobWarnings)...)
    83  
    84  	return allWarnings, err
    85  }
    86  
    87  func (actor Actor) GetOrganizationDomains(orgGuid string, labelSelector string) ([]resources.Domain, Warnings, error) {
    88  	keys := []ccv3.Query{}
    89  	if labelSelector != "" {
    90  		keys = append(keys, ccv3.Query{Key: ccv3.LabelSelectorFilter, Values: []string{labelSelector}})
    91  	}
    92  	ccv3Domains, warnings, err := actor.CloudControllerClient.GetOrganizationDomains(orgGuid, keys...)
    93  
    94  	if err != nil {
    95  		return nil, Warnings(warnings), err
    96  	}
    97  
    98  	var domains []resources.Domain
    99  	for _, domain := range ccv3Domains {
   100  		domains = append(domains, resources.Domain(domain))
   101  	}
   102  
   103  	return domains, Warnings(warnings), nil
   104  }
   105  
   106  func (actor Actor) GetDomain(domainGUID string) (resources.Domain, Warnings, error) {
   107  	domain, warnings, err := actor.CloudControllerClient.GetDomain(domainGUID)
   108  
   109  	if err != nil {
   110  		return resources.Domain{}, Warnings(warnings), err
   111  	}
   112  
   113  	return resources.Domain(domain), Warnings(warnings), nil
   114  }
   115  
   116  func (actor Actor) GetDomainByName(domainName string) (resources.Domain, Warnings, error) {
   117  	domains, warnings, err := actor.CloudControllerClient.GetDomains(
   118  		ccv3.Query{Key: ccv3.NameFilter, Values: []string{domainName}},
   119  	)
   120  
   121  	if err != nil {
   122  		return resources.Domain{}, Warnings(warnings), err
   123  	}
   124  
   125  	if len(domains) == 0 {
   126  		return resources.Domain{}, Warnings(warnings), actionerror.DomainNotFoundError{Name: domainName}
   127  	}
   128  
   129  	return resources.Domain(domains[0]), Warnings(warnings), nil
   130  }
   131  
   132  func (actor Actor) SharePrivateDomain(domainName string, orgName string) (Warnings, error) {
   133  	orgGUID, domainGUID, warnings, err := actor.GetDomainAndOrgGUIDsByName(domainName, orgName)
   134  
   135  	if err != nil {
   136  		return warnings, err
   137  	}
   138  
   139  	apiWarnings, err := actor.CloudControllerClient.SharePrivateDomainToOrgs(
   140  		domainGUID,
   141  		ccv3.SharedOrgs{GUIDs: []string{orgGUID}},
   142  	)
   143  
   144  	allWarnings := append(warnings, Warnings(apiWarnings)...)
   145  
   146  	return allWarnings, err
   147  }
   148  
   149  func (actor Actor) UnsharePrivateDomain(domainName string, orgName string) (Warnings, error) {
   150  	orgGUID, domainGUID, warnings, err := actor.GetDomainAndOrgGUIDsByName(domainName, orgName)
   151  
   152  	if err != nil {
   153  		return warnings, err
   154  	}
   155  
   156  	apiWarnings, err := actor.CloudControllerClient.UnsharePrivateDomainFromOrg(
   157  		domainGUID,
   158  		orgGUID,
   159  	)
   160  
   161  	allWarnings := append(warnings, Warnings(apiWarnings)...)
   162  
   163  	return allWarnings, err
   164  }
   165  
   166  func (actor Actor) GetDomainAndOrgGUIDsByName(domainName string, orgName string) (string, string, Warnings, error) {
   167  	org, getOrgWarnings, err := actor.GetOrganizationByName(orgName)
   168  
   169  	if err != nil {
   170  		return "", "", getOrgWarnings, err
   171  	}
   172  
   173  	domain, getDomainWarnings, err := actor.GetDomainByName(domainName)
   174  	allWarnings := append(getOrgWarnings, getDomainWarnings...)
   175  
   176  	if err != nil {
   177  		return "", "", allWarnings, err
   178  	}
   179  
   180  	return org.GUID, domain.GUID, allWarnings, nil
   181  }