utils.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. Copyright 2018 Google LLC
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package cloud
  14. import (
  15. "encoding/json"
  16. "fmt"
  17. "strings"
  18. "github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud/meta"
  19. )
  20. const (
  21. gaPrefix = "https://www.googleapis.com/compute/v1"
  22. alphaPrefix = "https://www.googleapis.com/compute/alpha"
  23. betaPrefix = "https://www.googleapis.com/compute/beta"
  24. )
  25. // ResourceID identifies a GCE resource as parsed from compute resource URL.
  26. type ResourceID struct {
  27. ProjectID string
  28. Resource string
  29. Key *meta.Key
  30. }
  31. // Equal returns true if two resource IDs are equal.
  32. func (r *ResourceID) Equal(other *ResourceID) bool {
  33. if r.ProjectID != other.ProjectID || r.Resource != other.Resource {
  34. return false
  35. }
  36. if r.Key != nil && other.Key != nil {
  37. return *r.Key == *other.Key
  38. }
  39. if r.Key == nil && other.Key == nil {
  40. return true
  41. }
  42. return false
  43. }
  44. // RelativeResourceName returns the relative resource name string
  45. // representing this ResourceID.
  46. func (r *ResourceID) RelativeResourceName() string {
  47. return RelativeResourceName(r.ProjectID, r.Resource, r.Key)
  48. }
  49. // ResourcePath returns the resource path representing this ResourceID.
  50. func (r *ResourceID) ResourcePath() string {
  51. return ResourcePath(r.Resource, r.Key)
  52. }
  53. func (r *ResourceID) SelfLink(ver meta.Version) string {
  54. return SelfLink(ver, r.ProjectID, r.Resource, r.Key)
  55. }
  56. // ParseResourceURL parses resource URLs of the following formats:
  57. //
  58. // global/<res>/<name>
  59. // regions/<region>/<res>/<name>
  60. // zones/<zone>/<res>/<name>
  61. // projects/<proj>
  62. // projects/<proj>/global/<res>/<name>
  63. // projects/<proj>/regions/<region>/<res>/<name>
  64. // projects/<proj>/zones/<zone>/<res>/<name>
  65. // [https://www.googleapis.com/compute/<ver>]/projects/<proj>/global/<res>/<name>
  66. // [https://www.googleapis.com/compute/<ver>]/projects/<proj>/regions/<region>/<res>/<name>
  67. // [https://www.googleapis.com/compute/<ver>]/projects/<proj>/zones/<zone>/<res>/<name>
  68. func ParseResourceURL(url string) (*ResourceID, error) {
  69. errNotValid := fmt.Errorf("%q is not a valid resource URL", url)
  70. // Trim prefix off URL leaving "projects/..."
  71. projectsIndex := strings.Index(url, "/projects/")
  72. if projectsIndex >= 0 {
  73. url = url[projectsIndex+1:]
  74. }
  75. parts := strings.Split(url, "/")
  76. if len(parts) < 2 || len(parts) > 6 {
  77. return nil, errNotValid
  78. }
  79. ret := &ResourceID{}
  80. scopedName := parts
  81. if parts[0] == "projects" {
  82. ret.Resource = "projects"
  83. ret.ProjectID = parts[1]
  84. scopedName = parts[2:]
  85. if len(scopedName) == 0 {
  86. return ret, nil
  87. }
  88. }
  89. switch scopedName[0] {
  90. case "global":
  91. if len(scopedName) != 3 {
  92. return nil, errNotValid
  93. }
  94. ret.Resource = scopedName[1]
  95. ret.Key = meta.GlobalKey(scopedName[2])
  96. return ret, nil
  97. case "regions":
  98. switch len(scopedName) {
  99. case 2:
  100. ret.Resource = "regions"
  101. ret.Key = meta.GlobalKey(scopedName[1])
  102. return ret, nil
  103. case 4:
  104. ret.Resource = scopedName[2]
  105. ret.Key = meta.RegionalKey(scopedName[3], scopedName[1])
  106. return ret, nil
  107. default:
  108. return nil, errNotValid
  109. }
  110. case "zones":
  111. switch len(scopedName) {
  112. case 2:
  113. ret.Resource = "zones"
  114. ret.Key = meta.GlobalKey(scopedName[1])
  115. return ret, nil
  116. case 4:
  117. ret.Resource = scopedName[2]
  118. ret.Key = meta.ZonalKey(scopedName[3], scopedName[1])
  119. return ret, nil
  120. default:
  121. return nil, errNotValid
  122. }
  123. }
  124. return nil, errNotValid
  125. }
  126. func copyViaJSON(dest, src interface{}) error {
  127. bytes, err := json.Marshal(src)
  128. if err != nil {
  129. return err
  130. }
  131. return json.Unmarshal(bytes, dest)
  132. }
  133. // ResourcePath returns the path starting from the location.
  134. // Example: regions/us-central1/subnetworks/my-subnet
  135. func ResourcePath(resource string, key *meta.Key) string {
  136. switch resource {
  137. case "zones", "regions":
  138. return fmt.Sprintf("%s/%s", resource, key.Name)
  139. case "projects":
  140. return "invalid-resource"
  141. }
  142. switch key.Type() {
  143. case meta.Zonal:
  144. return fmt.Sprintf("zones/%s/%s/%s", key.Zone, resource, key.Name)
  145. case meta.Regional:
  146. return fmt.Sprintf("regions/%s/%s/%s", key.Region, resource, key.Name)
  147. case meta.Global:
  148. return fmt.Sprintf("global/%s/%s", resource, key.Name)
  149. }
  150. return "invalid-key-type"
  151. }
  152. // RelativeResourceName returns the path starting from project.
  153. // Example: projects/my-project/regions/us-central1/subnetworks/my-subnet
  154. func RelativeResourceName(project, resource string, key *meta.Key) string {
  155. switch resource {
  156. case "projects":
  157. return fmt.Sprintf("projects/%s", project)
  158. default:
  159. return fmt.Sprintf("projects/%s/%s", project, ResourcePath(resource, key))
  160. }
  161. }
  162. // SelfLink returns the self link URL for the given object.
  163. func SelfLink(ver meta.Version, project, resource string, key *meta.Key) string {
  164. var prefix string
  165. switch ver {
  166. case meta.VersionAlpha:
  167. prefix = alphaPrefix
  168. case meta.VersionBeta:
  169. prefix = betaPrefix
  170. case meta.VersionGA:
  171. prefix = gaPrefix
  172. default:
  173. prefix = "invalid-prefix"
  174. }
  175. return fmt.Sprintf("%s/%s", prefix, RelativeResourceName(project, resource, key))
  176. }