github.com/jghiloni/cli@v6.28.1-0.20170628223758-0ce05fe032a2+incompatible/actor/v2action/organization.go (about) 1 package v2action 2 3 import ( 4 "fmt" 5 "strings" 6 7 "code.cloudfoundry.org/cli/api/cloudcontroller/ccerror" 8 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2" 9 ) 10 11 // Organization represents a CLI Organization. 12 type Organization ccv2.Organization 13 14 // OrganizationNotFoundError represents the scenario when the organization 15 // searched for could not be found. 16 type OrganizationNotFoundError struct { 17 GUID string 18 Name string 19 } 20 21 func (e OrganizationNotFoundError) Error() string { 22 return fmt.Sprintf("Organization '%s' not found.", e.Name) 23 } 24 25 // MultipleOrganizationsFoundError represents the scenario when the cloud 26 // controller returns multiple organizations when filtering by name. This is a 27 // far out edge case and should not happen. 28 type MultipleOrganizationsFoundError struct { 29 Name string 30 GUIDs []string 31 } 32 33 func (e MultipleOrganizationsFoundError) Error() string { 34 guids := strings.Join(e.GUIDs, ", ") 35 return fmt.Sprintf("Organization name '%s' matches multiple GUIDs: %s", e.Name, guids) 36 } 37 38 // GetOrganization returns an Organization based on the provided guid. 39 func (actor Actor) GetOrganization(guid string) (Organization, Warnings, error) { 40 org, warnings, err := actor.CloudControllerClient.GetOrganization(guid) 41 42 if _, ok := err.(ccerror.ResourceNotFoundError); ok { 43 return Organization{}, Warnings(warnings), OrganizationNotFoundError{GUID: guid} 44 } 45 46 return Organization(org), Warnings(warnings), err 47 } 48 49 // GetOrganizationByName returns an Organization based off of the name given. 50 func (actor Actor) GetOrganizationByName(orgName string) (Organization, Warnings, error) { 51 orgs, warnings, err := actor.CloudControllerClient.GetOrganizations([]ccv2.Query{ 52 { 53 Filter: ccv2.NameFilter, 54 Operator: ccv2.EqualOperator, 55 Value: orgName, 56 }, 57 }) 58 if err != nil { 59 return Organization{}, Warnings(warnings), err 60 } 61 62 if len(orgs) == 0 { 63 return Organization{}, Warnings(warnings), OrganizationNotFoundError{Name: orgName} 64 } 65 66 if len(orgs) > 1 { 67 var guids []string 68 for _, org := range orgs { 69 guids = append(guids, org.GUID) 70 } 71 return Organization{}, Warnings(warnings), MultipleOrganizationsFoundError{Name: orgName, GUIDs: guids} 72 } 73 74 return Organization(orgs[0]), Warnings(warnings), nil 75 } 76 77 // DeleteOrganization deletes the Organization associated with the provided 78 // GUID. Once the deletion request is sent, it polls the deletion job until 79 // it's finished. 80 func (actor Actor) DeleteOrganization(orgName string) (Warnings, error) { 81 var allWarnings Warnings 82 83 org, warnings, err := actor.GetOrganizationByName(orgName) 84 allWarnings = append(allWarnings, warnings...) 85 if err != nil { 86 return allWarnings, err 87 } 88 89 job, deleteWarnings, err := actor.CloudControllerClient.DeleteOrganization(org.GUID) 90 allWarnings = append(allWarnings, deleteWarnings...) 91 if err != nil { 92 return allWarnings, err 93 } 94 95 ccWarnings, err := actor.CloudControllerClient.PollJob(job) 96 for _, warning := range ccWarnings { 97 allWarnings = append(allWarnings, warning) 98 } 99 100 return allWarnings, err 101 }