raw.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 2019 Yunion
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package k8s
  15. import (
  16. "fmt"
  17. "io/ioutil"
  18. "strings"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/pkg/util/httputils"
  21. "yunion.io/x/onecloud/pkg/mcclient"
  22. )
  23. var RawResource *RawResourceManager
  24. func init() {
  25. RawResource = &RawResourceManager{
  26. serviceType: "k8s",
  27. }
  28. }
  29. type rawResource struct {
  30. kind string
  31. name string
  32. segs []string
  33. query jsonutils.JSONObject
  34. }
  35. func newRawResource(kind, namespace, name, cluster string) *rawResource {
  36. nsQuery := getNamespaceQuery(namespace)
  37. if cluster != "" {
  38. nsQuery.Add(jsonutils.NewString(cluster), "cluster")
  39. }
  40. ctx := &rawResource{kind: kind, name: name, segs: make([]string, 0), query: nsQuery}
  41. return ctx
  42. }
  43. func (ctx *rawResource) addSegs(seg ...string) {
  44. ctx.segs = append(ctx.segs, seg...)
  45. }
  46. func (ctx rawResource) path() string {
  47. segs := make([]string, 0)
  48. segs = append(segs, "_raw", ctx.kind, ctx.name)
  49. segs = append(segs, ctx.segs...)
  50. path := fmt.Sprintf("/%s", strings.Join(segs, "/"))
  51. if ctx.query != nil {
  52. qs := ctx.query.QueryString()
  53. if len(qs) > 0 {
  54. path = fmt.Sprintf("%s?%s", path, qs)
  55. }
  56. }
  57. return path
  58. }
  59. type RawResourceManager struct {
  60. serviceType string
  61. }
  62. func (m *RawResourceManager) request(s *mcclient.ClientSession, method httputils.THttpMethod, path string, body jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  63. _, ret, err := s.JSONRequest(m.serviceType, "", method, path, nil, body)
  64. return ret, err
  65. }
  66. func getNamespaceQuery(namespace string) *jsonutils.JSONDict {
  67. query := jsonutils.NewDict()
  68. if namespace != "" {
  69. query.Set("namespace", jsonutils.NewString(namespace))
  70. }
  71. return query
  72. }
  73. func (m *RawResourceManager) Get(s *mcclient.ClientSession, kind string, namespace string, name string, cluster string) (jsonutils.JSONObject, error) {
  74. ctx := newRawResource(kind, namespace, name, cluster)
  75. return m.request(s, "GET", ctx.path(), nil)
  76. }
  77. func (m *RawResourceManager) GetYAML(s *mcclient.ClientSession, kind string, namespace string, name string, cluster string) ([]byte, error) {
  78. ctx := newRawResource(kind, namespace, name, cluster)
  79. ctx.addSegs("yaml")
  80. resp, err := s.RawRequest(m.serviceType, "", "GET", ctx.path(), nil, nil)
  81. if err != nil {
  82. return nil, err
  83. }
  84. defer resp.Body.Close()
  85. return ioutil.ReadAll(resp.Body)
  86. }
  87. func (m *RawResourceManager) Put(s *mcclient.ClientSession, kind string, namespace string, name string, body jsonutils.JSONObject, cluster string) error {
  88. ctx := newRawResource(kind, namespace, name, cluster)
  89. _, err := m.request(s, "PUT", ctx.path(), body)
  90. return err
  91. }
  92. func (m *RawResourceManager) Delete(s *mcclient.ClientSession, kind string, namespace string, name string, cluster string) error {
  93. ctx := newRawResource(kind, namespace, name, cluster)
  94. _, err := m.request(s, "DELETE", ctx.path(), nil)
  95. return err
  96. }