github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/actor/v2action/organization.go (about) 1 package v2action 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/liamawhite/cli-with-i18n/api/cloudcontroller/ccerror" 8 "github.com/liamawhite/cli-with-i18n/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 Filter: ccv2.NameFilter, 53 Operator: ccv2.EqualOperator, 54 Values: []string{orgName}, 55 }) 56 if err != nil { 57 return Organization{}, Warnings(warnings), err 58 } 59 60 if len(orgs) == 0 { 61 return Organization{}, Warnings(warnings), OrganizationNotFoundError{Name: orgName} 62 } 63 64 if len(orgs) > 1 { 65 var guids []string 66 for _, org := range orgs { 67 guids = append(guids, org.GUID) 68 } 69 return Organization{}, Warnings(warnings), MultipleOrganizationsFoundError{Name: orgName, GUIDs: guids} 70 } 71 72 return Organization(orgs[0]), Warnings(warnings), nil 73 } 74 75 // DeleteOrganization deletes the Organization associated with the provided 76 // GUID. Once the deletion request is sent, it polls the deletion job until 77 // it's finished. 78 func (actor Actor) DeleteOrganization(orgName string) (Warnings, error) { 79 var allWarnings Warnings 80 81 org, warnings, err := actor.GetOrganizationByName(orgName) 82 allWarnings = append(allWarnings, warnings...) 83 if err != nil { 84 return allWarnings, err 85 } 86 87 job, deleteWarnings, err := actor.CloudControllerClient.DeleteOrganization(org.GUID) 88 allWarnings = append(allWarnings, deleteWarnings...) 89 if err != nil { 90 return allWarnings, err 91 } 92 93 ccWarnings, err := actor.CloudControllerClient.PollJob(job) 94 for _, warning := range ccWarnings { 95 allWarnings = append(allWarnings, warning) 96 } 97 98 return allWarnings, err 99 } 100 101 func (actor Actor) GetOrganizations() ([]Organization, Warnings, error) { 102 var returnedOrgs []Organization 103 orgs, warnings, err := actor.CloudControllerClient.GetOrganizations() 104 for _, org := range orgs { 105 returnedOrgs = append(returnedOrgs, Organization(org)) 106 } 107 return returnedOrgs, Warnings(warnings), err 108 }