github.com/randomtask1155/cli@v6.41.1-0.20181227003417-a98eed78cbde+incompatible/actor/v2action/service_access.go (about)

     1  package v2action
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant"
     9  )
    10  
    11  // EnableServiceForAllOrgs enables access for the given service in all orgs.
    12  func (actor Actor) EnableServiceForAllOrgs(serviceName string) (Warnings, error) {
    13  	servicePlans, allWarnings, err := actor.GetServicePlansForService(serviceName)
    14  	if err != nil {
    15  		return allWarnings, err
    16  	}
    17  
    18  	for _, plan := range servicePlans {
    19  		warnings, err := actor.CloudControllerClient.UpdateServicePlan(plan.GUID, true)
    20  		allWarnings = append(allWarnings, warnings...)
    21  		if err != nil {
    22  			return allWarnings, err
    23  		}
    24  	}
    25  
    26  	return allWarnings, nil
    27  }
    28  
    29  // EnablePlanForAllOrgs enables access to a specific plan of the given service in all orgs.
    30  func (actor Actor) EnablePlanForAllOrgs(serviceName, servicePlanName string) (Warnings, error) {
    31  	servicePlans, allWarnings, err := actor.GetServicePlansForService(serviceName)
    32  	if err != nil {
    33  		return allWarnings, err
    34  	}
    35  
    36  	// We delete all service plan visibilities for the given Plan since the attribute public should function as a giant on/off
    37  	// switch for all orgs. Thus we need to clean up any visibilities laying around so that they don't carry over.
    38  	for _, plan := range servicePlans {
    39  		if plan.Name == servicePlanName {
    40  			warnings, err := actor.removeOrgLevelServicePlanVisibilities(plan.GUID)
    41  			allWarnings = append(allWarnings, warnings...)
    42  			if err != nil {
    43  				return allWarnings, err
    44  			}
    45  
    46  			ccv2Warnings, err := actor.CloudControllerClient.UpdateServicePlan(plan.GUID, true)
    47  			allWarnings = append(allWarnings, ccv2Warnings...)
    48  			return allWarnings, err
    49  		}
    50  	}
    51  
    52  	return allWarnings, actionerror.ServicePlanNotFoundError{PlanName: servicePlanName, ServiceName: serviceName}
    53  }
    54  
    55  // EnableServiceForOrg enables access for the given service in a specific org.
    56  func (actor Actor) EnableServiceForOrg(serviceName, orgName string) (Warnings, error) {
    57  	servicePlans, allWarnings, err := actor.GetServicePlansForService(serviceName)
    58  	if err != nil {
    59  		return allWarnings, err
    60  	}
    61  
    62  	org, orgWarnings, err := actor.GetOrganizationByName(orgName)
    63  	allWarnings = append(allWarnings, orgWarnings...)
    64  	if err != nil {
    65  		return allWarnings, err
    66  	}
    67  
    68  	for _, plan := range servicePlans {
    69  		_, warnings, err := actor.CloudControllerClient.CreateServicePlanVisibility(plan.GUID, org.GUID)
    70  		allWarnings = append(allWarnings, warnings...)
    71  		if err != nil {
    72  			return allWarnings, err
    73  		}
    74  	}
    75  
    76  	return allWarnings, nil
    77  }
    78  
    79  // EnablePlanForOrg enables access to a specific plan of the given service in a specific org.
    80  func (actor Actor) EnablePlanForOrg(serviceName, servicePlanName, orgName string) (Warnings, error) {
    81  	servicePlans, allWarnings, err := actor.GetServicePlansForService(serviceName)
    82  	if err != nil {
    83  		return allWarnings, err
    84  	}
    85  
    86  	org, orgWarnings, err := actor.GetOrganizationByName(orgName)
    87  	allWarnings = append(allWarnings, orgWarnings...)
    88  	if err != nil {
    89  		return allWarnings, err
    90  	}
    91  
    92  	for _, plan := range servicePlans {
    93  		if plan.Name == servicePlanName {
    94  			_, warnings, err := actor.CloudControllerClient.CreateServicePlanVisibility(plan.GUID, org.GUID)
    95  			allWarnings = append(allWarnings, warnings...)
    96  			return allWarnings, err
    97  		}
    98  	}
    99  
   100  	return nil, fmt.Errorf("Service plan '%s' not found", servicePlanName)
   101  }
   102  
   103  func (actor Actor) removeOrgLevelServicePlanVisibilities(servicePlanGUID string) (Warnings, error) {
   104  	var allWarnings Warnings
   105  
   106  	visibilities, warnings, err := actor.CloudControllerClient.GetServicePlanVisibilities(ccv2.Filter{
   107  		Type:     constant.ServicePlanGUIDFilter,
   108  		Operator: constant.EqualOperator,
   109  		Values:   []string{servicePlanGUID},
   110  	})
   111  	allWarnings = append(allWarnings, warnings...)
   112  	if err != nil {
   113  		return allWarnings, err
   114  	}
   115  
   116  	for _, visibility := range visibilities {
   117  		warnings, err := actor.CloudControllerClient.DeleteServicePlanVisibility(visibility.GUID)
   118  		allWarnings = append(allWarnings, warnings...)
   119  		if err != nil {
   120  			return allWarnings, err
   121  		}
   122  	}
   123  
   124  	return allWarnings, nil
   125  }