object_user.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. Copyright 2018 The Rook Authors. All rights reserved.
  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. http://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 clients
  14. import (
  15. "context"
  16. "fmt"
  17. "strings"
  18. "github.com/rook/rook/pkg/daemon/ceph/client"
  19. rgw "github.com/rook/rook/pkg/operator/ceph/object"
  20. "github.com/rook/rook/tests/framework/installer"
  21. "github.com/rook/rook/tests/framework/utils"
  22. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  23. )
  24. // ObjectUserOperation is wrapper for k8s rook object user operations
  25. type ObjectUserOperation struct {
  26. k8sh *utils.K8sHelper
  27. manifests installer.CephManifests
  28. }
  29. // CreateObjectUserOperation creates new rook object user client
  30. func CreateObjectUserOperation(k8sh *utils.K8sHelper, manifests installer.CephManifests) *ObjectUserOperation {
  31. return &ObjectUserOperation{k8sh, manifests}
  32. }
  33. // ObjectUserGet Function to get the details of an object user from radosgw
  34. func (o *ObjectUserOperation) GetUser(namespace string, store string, userid string) (*rgw.ObjectUser, error) {
  35. ctx := o.k8sh.MakeContext()
  36. clusterInfo := client.AdminTestClusterInfo(namespace)
  37. objectStore, err := o.k8sh.RookClientset.CephV1().CephObjectStores(namespace).Get(context.TODO(), store, metav1.GetOptions{})
  38. if err != nil {
  39. return nil, fmt.Errorf("failed to get objectstore info: %+v", err)
  40. }
  41. rgwcontext, err := rgw.NewMultisiteContext(ctx, clusterInfo, objectStore)
  42. if err != nil {
  43. return nil, fmt.Errorf("failed to get RGW context: %+v", err)
  44. }
  45. userinfo, _, err := rgw.GetUser(rgwcontext, userid)
  46. if err != nil {
  47. return nil, fmt.Errorf("failed to get user info: %+v", err)
  48. }
  49. return userinfo, nil
  50. }
  51. // UserSecretExists Function to check that user secret was created
  52. func (o *ObjectUserOperation) UserSecretExists(namespace string, store string, userid string) bool {
  53. message, err := o.k8sh.GetResource("-n", namespace, "secrets", "-l", "rook_object_store="+store, "-l", "user="+userid)
  54. //GetResource(blah) returns success if blah is or is not found.
  55. //err = success and found_sec not "No resources found." means it was found
  56. //err = success and found_sec contains "No resources found." means it was not found
  57. //err != success is another error
  58. if err == nil && !strings.Contains(message, "No resources found") {
  59. logger.Infof("Object User Secret Exists")
  60. return true
  61. }
  62. logger.Infof("Unable to find user secret")
  63. return false
  64. }
  65. // ObjectUserCreate Function to create a object store user in rook
  66. func (o *ObjectUserOperation) Create(userid, displayName, store, usercaps, maxsize string, maxbuckets, maxobjects int) error {
  67. logger.Infof("creating the object store user via CRD")
  68. if err := o.k8sh.ResourceOperation("apply", o.manifests.GetObjectStoreUser(userid, displayName, store, usercaps, maxsize, maxbuckets, maxobjects)); err != nil {
  69. return err
  70. }
  71. return nil
  72. }
  73. func (o *ObjectUserOperation) Delete(namespace string, userid string) error {
  74. logger.Infof("Deleting the object store user via CRD")
  75. if err := o.k8sh.DeleteResource("-n", namespace, "CephObjectStoreUser", userid); err != nil {
  76. return err
  77. }
  78. return nil
  79. }