Skip to content

Commit

Permalink
PANDARIA: update huawei/tencent cloud credential
Browse files Browse the repository at this point in the history
  • Loading branch information
JacieChao committed Sep 27, 2024
1 parent 27db041 commit a9e3b51
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 29 deletions.
47 changes: 32 additions & 15 deletions extensions/cloudcredentials/huawei/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
43 changes: 29 additions & 14 deletions extensions/cloudcredentials/tencent/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 2 additions & 0 deletions extensions/defaults/providers/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ const (
Google = "google"
Vsphere = "vsphere"
Aliyun = "aliyun"
Huawei = "huawei"
Tencent = "tke"
)

0 comments on commit a9e3b51

Please sign in to comment.