diff --git a/extensions/cloudcredentials/huawei/create.go b/extensions/cloudcredentials/huawei/create.go index fc8e5de0..03747304 100644 --- a/extensions/cloudcredentials/huawei/create.go +++ b/extensions/cloudcredentials/huawei/create.go @@ -2,28 +2,45 @@ package huawei import ( "github.com/rancher/shepherd/clients/rancher" - management "github.com/rancher/shepherd/clients/rancher/generated/management/v3" + v1 "github.com/rancher/shepherd/clients/rancher/v1" "github.com/rancher/shepherd/extensions/cloudcredentials" - "github.com/rancher/shepherd/pkg/config" + "github.com/rancher/shepherd/extensions/defaults" + "github.com/rancher/shepherd/extensions/defaults/namespaces" + "github.com/rancher/shepherd/extensions/defaults/providers" + "github.com/rancher/shepherd/extensions/defaults/stevetypes" + "github.com/rancher/shepherd/extensions/steve" + "github.com/rancher/shepherd/pkg/namegenerator" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -const huaweiCloudCredNameBase = "huaweiCloudCredential" - // CreateHuaweiCloudCredentials is a helper function that takes the rancher Client as a parameter and creates // an Huawei cloud credential, and returns the CloudCredential response -func CreateHuaweiCloudCredentials(rancherClient *rancher.Client) (*cloudcredentials.CloudCredential, error) { - var huaweiCredentialConfig cloudcredentials.HuaweiCredentialConfig - config.LoadConfig(cloudcredentials.HuaweiCredentialConfigurationFileKey, &huaweiCredentialConfig) - - cloudCredential := cloudcredentials.CloudCredential{ - Name: huaweiCloudCredNameBase, - HuaweiCredentialConfig: &huaweiCredentialConfig, +func CreateHuaweiCloudCredentials(client *rancher.Client, credentials cloudcredentials.CloudCredential) (*v1.SteveAPIObject, error) { + secretName := namegenerator.AppendRandomString(providers.Huawei) + spec := corev1.Secret{ + ObjectMeta: metav1.ObjectMeta{ + GenerateName: cloudcredentials.GeneratedName, + Namespace: namespaces.CattleData, + Annotations: map[string]string{ + "provisioning.cattle.io/driver": providers.Huawei, + "field.cattle.io/name": secretName, + "field.cattle.io/creatorId": client.UserID, + }, + }, + Data: map[string][]byte{ + "huaweicredentialConfig-accessKey": []byte(credentials.HuaweiCredentialConfig.AccessKey), + "huaweicredentialConfig-secretKey": []byte(credentials.HuaweiCredentialConfig.SecretKey), + "huaweicredentialConfig-regionID": []byte(credentials.HuaweiCredentialConfig.RegionID), + "huaweicredentialConfig-projectID": []byte(credentials.HuaweiCredentialConfig.ProjectID), + }, + Type: corev1.SecretTypeOpaque, } - resp := &cloudcredentials.CloudCredential{} - err := rancherClient.Management.APIBaseClient.Ops.DoCreate(management.CloudCredentialType, cloudCredential, resp) + huaweiCloudCredentials, err := steve.CreateAndWaitForResource(client, stevetypes.Secret, spec, true, defaults.FiveSecondTimeout, defaults.FiveMinuteTimeout) if err != nil { return nil, err } - return resp, nil -} + + return huaweiCloudCredentials, nil +} \ No newline at end of file diff --git a/extensions/cloudcredentials/tencent/create.go b/extensions/cloudcredentials/tencent/create.go index f7d7d94c..d271b265 100644 --- a/extensions/cloudcredentials/tencent/create.go +++ b/extensions/cloudcredentials/tencent/create.go @@ -2,28 +2,43 @@ package tencent import ( "github.com/rancher/shepherd/clients/rancher" - management "github.com/rancher/shepherd/clients/rancher/generated/management/v3" + v1 "github.com/rancher/shepherd/clients/rancher/v1" "github.com/rancher/shepherd/extensions/cloudcredentials" - "github.com/rancher/shepherd/pkg/config" + "github.com/rancher/shepherd/extensions/defaults" + "github.com/rancher/shepherd/extensions/defaults/namespaces" + "github.com/rancher/shepherd/extensions/defaults/providers" + "github.com/rancher/shepherd/extensions/defaults/stevetypes" + "github.com/rancher/shepherd/extensions/steve" + "github.com/rancher/shepherd/pkg/namegenerator" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -const tencentCloudCredNameBase = "tencentCloudCredential" - // CreateTencentCloudCredentials is a helper function that takes the rancher Client as a parameter and creates // an Tencent cloud credential, and returns the CloudCredential response -func CreateTencentCloudCredentials(rancherClient *rancher.Client) (*cloudcredentials.CloudCredential, error) { - var tencentCredentialConfig cloudcredentials.TencentCredentialConfig - config.LoadConfig(cloudcredentials.TencentCredentialConfigurationFileKey, &tencentCredentialConfig) - - cloudCredential := cloudcredentials.CloudCredential{ - Name: tencentCloudCredNameBase, - TencentCredentialConfig: &tencentCredentialConfig, +func CreateTencentCloudCredentials(client *rancher.Client, credentials cloudcredentials.CloudCredential) (*v1.SteveAPIObject, error) { + secretName := namegenerator.AppendRandomString(providers.Tencent) + spec := corev1.Secret{ + ObjectMeta: metav1.ObjectMeta{ + GenerateName: cloudcredentials.GeneratedName, + Namespace: namespaces.CattleData, + Annotations: map[string]string{ + "provisioning.cattle.io/driver": providers.Tencent, + "field.cattle.io/name": secretName, + "field.cattle.io/creatorId": client.UserID, + }, + }, + Data: map[string][]byte{ + "tkecredentialConfig-accessKeyId": []byte(credentials.TencentCredentialConfig.AccessKeyID), + "tkecredentialConfig-accessKeySecret": []byte(credentials.TencentCredentialConfig.AccessKeySecret), + }, + Type: corev1.SecretTypeOpaque, } - resp := &cloudcredentials.CloudCredential{} - err := rancherClient.Management.APIBaseClient.Ops.DoCreate(management.CloudCredentialType, cloudCredential, resp) + tkeCloudCredentials, err := steve.CreateAndWaitForResource(client, stevetypes.Secret, spec, true, defaults.FiveSecondTimeout, defaults.FiveMinuteTimeout) if err != nil { return nil, err } - return resp, nil + + return tkeCloudCredentials, nil } diff --git a/extensions/defaults/providers/providers.go b/extensions/defaults/providers/providers.go index 69670129..198162c5 100644 --- a/extensions/defaults/providers/providers.go +++ b/extensions/defaults/providers/providers.go @@ -9,4 +9,6 @@ const ( Google = "google" Vsphere = "vsphere" Aliyun = "aliyun" + Huawei = "huawei" + Tencent = "tke" )