github.com/swisscom/cloudfoundry-cli@v7.1.0+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/ccerror"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant"
    10  )
    11  
    12  // EnableServiceForAllOrgs enables access for the given service in all orgs.
    13  func (actor Actor) EnableServiceForAllOrgs(serviceName, brokerName string) (Warnings, error) {
    14  	servicePlans, allWarnings, err := actor.GetServicePlansForService(serviceName, brokerName)
    15  	if err != nil {
    16  		return allWarnings, err
    17  	}
    18  
    19  	for _, plan := range servicePlans {
    20  		warnings, err := actor.removeOrgLevelServicePlanVisibilities(plan.GUID, "")
    21  		allWarnings = append(allWarnings, warnings...)
    22  		if err != nil {
    23  			return allWarnings, err
    24  		}
    25  
    26  		ccv2Warnings, err := actor.CloudControllerClient.UpdateServicePlan(plan.GUID, true)
    27  		allWarnings = append(allWarnings, ccv2Warnings...)
    28  		if err != nil {
    29  			return allWarnings, err
    30  		}
    31  	}
    32  
    33  	return allWarnings, nil
    34  }
    35  
    36  // EnablePlanForAllOrgs enables access to a specific plan of the given service in all orgs.
    37  func (actor Actor) EnablePlanForAllOrgs(serviceName, servicePlanName, brokerName string) (Warnings, error) {
    38  	servicePlans, allWarnings, err := actor.GetServicePlansForService(serviceName, brokerName)
    39  	if err != nil {
    40  		return allWarnings, err
    41  	}
    42  
    43  	// We delete all service plan visibilities for the given Plan since the attribute public should function as a giant on/off
    44  	// switch for all orgs. Thus we need to clean up any visibilities laying around so that they don't carry over.
    45  	for _, plan := range servicePlans {
    46  		if plan.Name == servicePlanName {
    47  			warnings, err := actor.removeOrgLevelServicePlanVisibilities(plan.GUID, "")
    48  			allWarnings = append(allWarnings, warnings...)
    49  			if err != nil {
    50  				return allWarnings, err
    51  			}
    52  
    53  			ccv2Warnings, err := actor.CloudControllerClient.UpdateServicePlan(plan.GUID, true)
    54  			allWarnings = append(allWarnings, ccv2Warnings...)
    55  			return allWarnings, err
    56  		}
    57  	}
    58  
    59  	return allWarnings, actionerror.ServicePlanNotFoundError{PlanName: servicePlanName, OfferingName: serviceName}
    60  }
    61  
    62  // EnableServiceForOrg enables access for the given service in a specific org.
    63  func (actor Actor) EnableServiceForOrg(serviceName, orgName, brokerName string) (Warnings, error) {
    64  	servicePlans, allWarnings, err := actor.GetServicePlansForService(serviceName, brokerName)
    65  	if err != nil {
    66  		return allWarnings, err
    67  	}
    68  
    69  	org, orgWarnings, err := actor.GetOrganizationByName(orgName)
    70  	allWarnings = append(allWarnings, orgWarnings...)
    71  	if err != nil {
    72  		return allWarnings, err
    73  	}
    74  
    75  	for _, plan := range servicePlans {
    76  		if plan.Public != true {
    77  			_, warnings, err := actor.CloudControllerClient.CreateServicePlanVisibility(plan.GUID, org.GUID)
    78  			allWarnings = append(allWarnings, warnings...)
    79  			if _, alreadyExistsError := err.(ccerror.ServicePlanVisibilityExistsError); alreadyExistsError {
    80  				return allWarnings, nil
    81  			}
    82  			if err != nil {
    83  				return allWarnings, err
    84  			}
    85  		}
    86  	}
    87  
    88  	return allWarnings, nil
    89  }
    90  
    91  // EnablePlanForOrg enables access to a specific plan of the given service in a specific org.
    92  func (actor Actor) EnablePlanForOrg(serviceName, servicePlanName, orgName, brokerName string) (Warnings, error) {
    93  	servicePlans, allWarnings, err := actor.GetServicePlansForService(serviceName, brokerName)
    94  	if err != nil {
    95  		return allWarnings, err
    96  	}
    97  
    98  	org, orgWarnings, err := actor.GetOrganizationByName(orgName)
    99  	allWarnings = append(allWarnings, orgWarnings...)
   100  	if err != nil {
   101  		return allWarnings, err
   102  	}
   103  
   104  	for _, plan := range servicePlans {
   105  		if plan.Name == servicePlanName {
   106  			if plan.Public {
   107  				return allWarnings, nil
   108  			}
   109  			_, warnings, err := actor.CloudControllerClient.CreateServicePlanVisibility(plan.GUID, org.GUID)
   110  			allWarnings = append(allWarnings, warnings...)
   111  			if _, alreadyExistsError := err.(ccerror.ServicePlanVisibilityExistsError); alreadyExistsError {
   112  				return allWarnings, nil
   113  			}
   114  			return allWarnings, err
   115  		}
   116  	}
   117  
   118  	return nil, fmt.Errorf("Service plan '%s' not found", servicePlanName)
   119  }
   120  
   121  // DisableServiceForAllOrgs disables access for the given service in all orgs.
   122  func (actor Actor) DisableServiceForAllOrgs(serviceName, brokerName string) (Warnings, error) {
   123  	servicePlans, allWarnings, err := actor.GetServicePlansForService(serviceName, brokerName)
   124  	if err != nil {
   125  		return allWarnings, err
   126  	}
   127  
   128  	for _, plan := range servicePlans {
   129  		warnings, err := actor.removeOrgLevelServicePlanVisibilities(plan.GUID, "")
   130  		allWarnings = append(allWarnings, warnings...)
   131  		if err != nil {
   132  			return allWarnings, err
   133  		}
   134  		if plan.Public == true {
   135  			warnings, err := actor.CloudControllerClient.UpdateServicePlan(plan.GUID, false)
   136  			allWarnings = append(allWarnings, warnings...)
   137  			if err != nil {
   138  				return allWarnings, err
   139  			}
   140  		}
   141  	}
   142  
   143  	return allWarnings, nil
   144  }
   145  
   146  // DisablePlanForAllOrgs disables access to a specific plan of the given service, from the given broker in all orgs.
   147  func (actor Actor) DisablePlanForAllOrgs(serviceName, servicePlanName, brokerName string) (Warnings, error) {
   148  	servicePlans, allWarnings, err := actor.GetServicePlansForService(serviceName, brokerName)
   149  	if err != nil {
   150  		return allWarnings, err
   151  	}
   152  
   153  	planFound := false
   154  	for _, plan := range servicePlans {
   155  		if plan.Name == servicePlanName {
   156  			warnings, err := actor.removeOrgLevelServicePlanVisibilities(plan.GUID, "")
   157  			allWarnings = append(allWarnings, warnings...)
   158  			if err != nil {
   159  				return allWarnings, err
   160  			}
   161  
   162  			if plan.Public == true {
   163  				ccv2Warnings, err := actor.CloudControllerClient.UpdateServicePlan(plan.GUID, false)
   164  				allWarnings = append(allWarnings, ccv2Warnings...)
   165  				return allWarnings, err
   166  			}
   167  			planFound = true
   168  			break
   169  		}
   170  	}
   171  
   172  	if planFound == false {
   173  		return allWarnings, actionerror.ServicePlanNotFoundError{PlanName: servicePlanName, OfferingName: serviceName}
   174  	}
   175  	return allWarnings, nil
   176  }
   177  
   178  // DisableServiceForOrg disables access for the given service in a specific org.
   179  func (actor Actor) DisableServiceForOrg(serviceName, orgName, brokerName string) (Warnings, error) {
   180  	servicePlans, allWarnings, err := actor.GetServicePlansForService(serviceName, brokerName)
   181  	if err != nil {
   182  		return allWarnings, err
   183  	}
   184  
   185  	org, orgWarnings, err := actor.GetOrganizationByName(orgName)
   186  	allWarnings = append(allWarnings, orgWarnings...)
   187  	if err != nil {
   188  		return allWarnings, err
   189  	}
   190  
   191  	for _, plan := range servicePlans {
   192  		warnings, err := actor.removeOrgLevelServicePlanVisibilities(plan.GUID, org.GUID)
   193  		allWarnings = append(allWarnings, warnings...)
   194  		if err != nil {
   195  			return allWarnings, err
   196  		}
   197  	}
   198  	return allWarnings, nil
   199  }
   200  
   201  // DisablePlanForOrg disables access to a specific plan of the given service from the given broker in a specific org.
   202  func (actor Actor) DisablePlanForOrg(serviceName, servicePlanName, orgName, brokerName string) (Warnings, error) {
   203  	servicePlans, allWarnings, err := actor.GetServicePlansForService(serviceName, brokerName)
   204  	if err != nil {
   205  		return allWarnings, err
   206  	}
   207  
   208  	org, orgWarnings, err := actor.GetOrganizationByName(orgName)
   209  	allWarnings = append(allWarnings, orgWarnings...)
   210  	if err != nil {
   211  		return allWarnings, err
   212  	}
   213  
   214  	planFound := false
   215  	for _, plan := range servicePlans {
   216  		if plan.Name == servicePlanName {
   217  			warnings, err := actor.removeOrgLevelServicePlanVisibilities(plan.GUID, org.GUID)
   218  			allWarnings = append(allWarnings, warnings...)
   219  			if err != nil {
   220  				return allWarnings, err
   221  			}
   222  			planFound = true
   223  			break
   224  		}
   225  	}
   226  
   227  	if planFound == false {
   228  		return allWarnings, actionerror.ServicePlanNotFoundError{PlanName: servicePlanName, OfferingName: serviceName}
   229  	}
   230  	return allWarnings, nil
   231  }
   232  
   233  func (actor Actor) removeOrgLevelServicePlanVisibilities(servicePlanGUID, organizationGUID string) (Warnings, error) {
   234  	filters := []ccv2.Filter{
   235  		{
   236  			Type:     constant.ServicePlanGUIDFilter,
   237  			Operator: constant.EqualOperator,
   238  			Values:   []string{servicePlanGUID},
   239  		},
   240  	}
   241  	if organizationGUID != "" {
   242  		filters = append(filters, ccv2.Filter{
   243  			Type:     constant.OrganizationGUIDFilter,
   244  			Operator: constant.EqualOperator,
   245  			Values:   []string{organizationGUID},
   246  		})
   247  	}
   248  
   249  	var allWarnings Warnings
   250  
   251  	visibilities, warnings, err := actor.CloudControllerClient.GetServicePlanVisibilities(filters...)
   252  	allWarnings = append(allWarnings, warnings...)
   253  	if err != nil {
   254  		return allWarnings, err
   255  	}
   256  
   257  	for _, visibility := range visibilities {
   258  		warnings, err := actor.CloudControllerClient.DeleteServicePlanVisibility(visibility.GUID)
   259  		allWarnings = append(allWarnings, warnings...)
   260  		if err != nil {
   261  			return allWarnings, err
   262  		}
   263  	}
   264  
   265  	return allWarnings, nil
   266  }