github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/utils/utils.go (about) 1 package utils 2 3 import ( 4 "fmt" 5 "net/url" 6 "strings" 7 8 "github.com/IBM-Cloud/bluemix-go/api/iampap/iampapv2" 9 "github.com/IBM-Cloud/bluemix-go/bmxerror" 10 "github.com/IBM-Cloud/bluemix-go/models" 11 12 "github.com/IBM-Cloud/bluemix-go/crn" 13 ) 14 15 func GetLocationFromTargetCRN(crnResource string) string { 16 if strings.HasPrefix(crnResource, "bluemix-") { 17 return crnResource[len("bluemix-"):] 18 } else if strings.HasPrefix(crnResource, "staging-") { 19 return crnResource[len("staging-"):] 20 } else { 21 return crnResource 22 } 23 } 24 25 func GenerateSpaceCRN(region models.Region, orgID string, spaceID string) crn.CRN { 26 spaceCRN := crn.New(CloudName(region), CloudType(region)) 27 spaceCRN.ServiceName = crn.ServiceBluemix 28 spaceCRN.Region = region.Name 29 spaceCRN.ScopeType = crn.ScopeOrganization 30 spaceCRN.Scope = orgID 31 spaceCRN.ResourceType = crn.ResourceTypeCFSpace 32 spaceCRN.Resource = spaceID 33 return spaceCRN 34 } 35 36 func CloudName(region models.Region) string { 37 regionID := region.ID 38 if regionID == "" { 39 return "" 40 } 41 42 splits := strings.Split(regionID, ":") 43 if len(splits) != 3 { 44 return "" 45 } 46 47 customer := splits[0] 48 if customer != "ibm" { 49 return customer 50 } 51 52 deployment := splits[1] 53 switch { 54 case deployment == "yp": 55 return "bluemix" 56 case strings.HasPrefix(deployment, "ys"): 57 return "staging" 58 default: 59 return "" 60 } 61 } 62 63 func CloudType(region models.Region) string { 64 return region.Type 65 } 66 67 func GenerateBoundToCRN(region models.Region, accountID string) crn.CRN { 68 var boundTo crn.CRN 69 if region.Type == "dedicated" { 70 // cname and ctype are hard coded for dedicated 71 boundTo = crn.New("bluemix", "public") 72 } else { 73 boundTo = crn.New(CloudName(region), CloudType(region)) 74 } 75 76 boundTo.ScopeType = crn.ScopeAccount 77 boundTo.Scope = accountID 78 return boundTo 79 } 80 81 func GetRolesFromRoleNamesV2(roleNames []string, roles []iampapv2.Role) ([]iampapv2.Role, error) { 82 83 filteredRoles := []iampapv2.Role{} 84 for _, roleName := range roleNames { 85 role, err := FindRoleByNameV2(roles, roleName) 86 if err != nil { 87 return []iampapv2.Role{}, err 88 } 89 filteredRoles = append(filteredRoles, role) 90 } 91 return filteredRoles, nil 92 } 93 func FindRoleByNameV2(supported []iampapv2.Role, name string) (iampapv2.Role, error) { 94 for _, role := range supported { 95 if role.DisplayName == name { 96 return role, nil 97 } 98 } 99 supportedRoles := getSupportedRolesStringV2(supported) 100 return iampapv2.Role{}, bmxerror.New(ErrCodeRRoleDoesnotExist, 101 fmt.Sprintf("%s was not found. Valid roles are %s", name, supportedRoles)) 102 103 } 104 105 func getSupportedRolesStringV2(supported []iampapv2.Role) string { 106 rolesStr := "" 107 for index, role := range supported { 108 if index != 0 { 109 rolesStr += ", " 110 } 111 rolesStr += role.DisplayName 112 } 113 return rolesStr 114 } 115 116 func GetRolesFromRoleNames(roleNames []string, roles []models.PolicyRole) ([]models.PolicyRole, error) { 117 118 filteredRoles := []models.PolicyRole{} 119 for _, roleName := range roleNames { 120 role, err := FindRoleByName(roles, roleName) 121 if err != nil { 122 return []models.PolicyRole{}, err 123 } 124 filteredRoles = append(filteredRoles, role) 125 } 126 return filteredRoles, nil 127 } 128 129 const ErrCodeRRoleDoesnotExist = "RoleDoesnotExist" 130 131 func FindRoleByName(supported []models.PolicyRole, name string) (models.PolicyRole, error) { 132 for _, role := range supported { 133 if role.DisplayName == name { 134 return role, nil 135 } 136 } 137 supportedRoles := getSupportedRolesString(supported) 138 return models.PolicyRole{}, bmxerror.New(ErrCodeRRoleDoesnotExist, 139 fmt.Sprintf("%s was not found. Valid roles are %s", name, supportedRoles)) 140 141 } 142 143 func getSupportedRolesString(supported []models.PolicyRole) string { 144 rolesStr := "" 145 for index, role := range supported { 146 if index != 0 { 147 rolesStr += ", " 148 } 149 rolesStr += role.DisplayName 150 } 151 return rolesStr 152 } 153 154 func EscapeUrlParm(urlParm string) string { 155 if strings.Contains(urlParm, "/") { 156 newUrlParm := url.PathEscape(urlParm) 157 return newUrlParm 158 } 159 return urlParm 160 }