github.com/swisscom/cloudfoundry-cli@v7.1.0+incompatible/actor/v2action/organization.go (about) 1 package v2action 2 3 import ( 4 "code.cloudfoundry.org/cli/actor/actionerror" 5 "code.cloudfoundry.org/cli/api/cloudcontroller/ccerror" 6 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2" 7 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant" 8 uaaconst "code.cloudfoundry.org/cli/api/uaa/constant" 9 ) 10 11 // Organization represents a CLI Organization. 12 type Organization ccv2.Organization 13 14 // GetOrganization returns an Organization based on the provided guid. 15 func (actor Actor) GetOrganization(guid string) (Organization, Warnings, error) { 16 org, warnings, err := actor.CloudControllerClient.GetOrganization(guid) 17 18 if _, ok := err.(ccerror.ResourceNotFoundError); ok { 19 return Organization{}, Warnings(warnings), actionerror.OrganizationNotFoundError{GUID: guid} 20 } 21 22 return Organization(org), Warnings(warnings), err 23 } 24 25 // GetOrganizationByName returns an Organization based off of the name given. 26 func (actor Actor) GetOrganizationByName(orgName string) (Organization, Warnings, error) { 27 orgs, warnings, err := actor.CloudControllerClient.GetOrganizations(ccv2.Filter{ 28 Type: constant.NameFilter, 29 Operator: constant.EqualOperator, 30 Values: []string{orgName}, 31 }) 32 if err != nil { 33 return Organization{}, Warnings(warnings), err 34 } 35 36 if len(orgs) == 0 { 37 return Organization{}, Warnings(warnings), actionerror.OrganizationNotFoundError{Name: orgName} 38 } 39 40 if len(orgs) > 1 { 41 var guids []string 42 for _, org := range orgs { 43 guids = append(guids, org.GUID) 44 } 45 return Organization{}, Warnings(warnings), actionerror.MultipleOrganizationsFoundError{Name: orgName, GUIDs: guids} 46 } 47 48 return Organization(orgs[0]), Warnings(warnings), nil 49 } 50 51 // GrantOrgManagerByUsername gives the Org Manager role to the provided user. 52 func (actor Actor) GrantOrgManagerByUsername(guid string, username string) (Warnings, error) { 53 var warnings ccv2.Warnings 54 var err error 55 56 if actor.Config.UAAGrantType() != string(uaaconst.GrantTypeClientCredentials) { 57 warnings, err = actor.CloudControllerClient.UpdateOrganizationManagerByUsername(guid, username) 58 } else { 59 warnings, err = actor.CloudControllerClient.UpdateOrganizationManager(guid, username) 60 } 61 62 return Warnings(warnings), err 63 } 64 65 // CreateOrganization creates an Organization based on the provided orgName. 66 func (actor Actor) CreateOrganization(orgName string, quotaName string) (Organization, Warnings, error) { 67 var quotaGUID string 68 var allWarnings Warnings 69 70 if quotaName != "" { 71 quota, warnings, err := actor.GetOrganizationQuotaByName(quotaName) 72 73 allWarnings = append(allWarnings, warnings...) 74 if err != nil { 75 return Organization{}, allWarnings, err 76 } 77 78 quotaGUID = quota.GUID 79 } 80 81 org, warnings, err := actor.CloudControllerClient.CreateOrganization(orgName, quotaGUID) 82 allWarnings = append(allWarnings, warnings...) 83 if _, ok := err.(ccerror.OrganizationNameTakenError); ok { 84 return Organization{}, allWarnings, actionerror.OrganizationNameTakenError{Name: orgName} 85 } 86 if err != nil { 87 return Organization{}, allWarnings, err 88 } 89 90 return Organization(org), allWarnings, nil 91 } 92 93 // DeleteOrganization deletes the Organization associated with the provided 94 // GUID. Once the deletion request is sent, it polls the deletion job until 95 // it's finished. 96 func (actor Actor) DeleteOrganization(orgName string) (Warnings, error) { 97 var allWarnings Warnings 98 99 org, warnings, err := actor.GetOrganizationByName(orgName) 100 allWarnings = append(allWarnings, warnings...) 101 if err != nil { 102 return allWarnings, err 103 } 104 105 job, deleteWarnings, err := actor.CloudControllerClient.DeleteOrganizationJob(org.GUID) 106 allWarnings = append(allWarnings, deleteWarnings...) 107 if err != nil { 108 return allWarnings, err 109 } 110 111 ccWarnings, err := actor.CloudControllerClient.PollJob(job) 112 for _, warning := range ccWarnings { 113 allWarnings = append(allWarnings, warning) 114 } 115 116 return allWarnings, err 117 } 118 119 // GetOrganizations returns all the available organizations. 120 func (actor Actor) GetOrganizations() ([]Organization, Warnings, error) { 121 var returnedOrgs []Organization 122 orgs, warnings, err := actor.CloudControllerClient.GetOrganizations() 123 for _, org := range orgs { 124 returnedOrgs = append(returnedOrgs, Organization(org)) 125 } 126 return returnedOrgs, Warnings(warnings), err 127 } 128 129 // OrganizationExistsWithName returns true if there is an Organization with the 130 // provided name, otherwise false. 131 func (actor Actor) OrganizationExistsWithName(orgName string) (bool, Warnings, error) { 132 orgs, warnings, err := actor.CloudControllerClient.GetOrganizations(ccv2.Filter{ 133 Type: constant.NameFilter, 134 Operator: constant.EqualOperator, 135 Values: []string{orgName}, 136 }) 137 if err != nil { 138 return false, Warnings(warnings), err 139 } 140 141 if len(orgs) == 0 { 142 return false, Warnings(warnings), nil 143 } 144 145 return true, Warnings(warnings), nil 146 }