diff --git a/apis/types/types.go b/apis/types/types.go index 01ccc1166..1936a0dfe 100644 --- a/apis/types/types.go +++ b/apis/types/types.go @@ -73,8 +73,8 @@ const ( LabelConfigSyncToMultiCluster = "config.oam.dev/multi-cluster" // LabelConfigIdentifier is the label for config identifier LabelConfigIdentifier = "config.oam.dev/identifier" - // LabelConfigDescription is the label for config description - LabelConfigDescription = "config.oam.dev/description" + // AnnotationConfigDescription is the annotation for config description + AnnotationConfigDescription = "config.oam.dev/description" // AnnotationConfigAlias is the annotation for config alias AnnotationConfigAlias = "config.oam.dev/alias" ) @@ -148,3 +148,8 @@ const ( // HelmRepository is the config type for Helm chart repository HelmRepository = "config-helm-repository" ) + +const ( + // TerrfaormComponentPrefix is the prefix of component type of terraform-xxx + TerrfaormComponentPrefix = "terraform-" +) diff --git a/pkg/apiserver/rest/apis/v1/types.go b/pkg/apiserver/rest/apis/v1/types.go index 9a28ee9d9..484dc6d02 100644 --- a/pkg/apiserver/rest/apis/v1/types.go +++ b/pkg/apiserver/rest/apis/v1/types.go @@ -202,6 +202,7 @@ type Config struct { Name string `json:"name"` Project string `json:"project"` Identifier string `json:"identifier"` + Alias string `json:"alias"` Description string `json:"description"` CreatedTime *time.Time `json:"createdTime"` UpdatedTime *time.Time `json:"updatedTime"` @@ -411,6 +412,7 @@ type CreateApplicationRequest struct { type CreateConfigRequest struct { Name string `json:"name" validate:"checkname"` Alias string `json:"alias"` + Description string `json:"description"` Project string `json:"project"` ComponentType string `json:"componentType" validate:"checkname"` Properties string `json:"properties,omitempty"` diff --git a/pkg/apiserver/rest/usecase/config.go b/pkg/apiserver/rest/usecase/config.go index 984693fd2..ea60f9881 100644 --- a/pkg/apiserver/rest/usecase/config.go +++ b/pkg/apiserver/rest/usecase/config.go @@ -20,6 +20,7 @@ import ( "context" "encoding/json" "fmt" + "strings" set "github.com/deckarep/golang-set" "github.com/pkg/errors" @@ -134,12 +135,27 @@ func (u *configUseCaseImpl) GetConfigType(ctx context.Context, configType string } func (u *configUseCaseImpl) CreateConfig(ctx context.Context, req apis.CreateConfigRequest) error { + p := req.Properties + // If the component is Terraform type, set the provider name same as the application name and the component name + if strings.HasPrefix(req.ComponentType, types.TerrfaormComponentPrefix) { + var properties map[string]interface{} + if err := json.Unmarshal([]byte(p), &properties); err != nil { + return errors.Wrapf(err, "unable to process the properties of %s", req.ComponentType) + } + properties["name"] = req.Name + tmp, err := json.Marshal(properties) + if err != nil { + return errors.Wrapf(err, "unable to process the properties of %s", req.ComponentType) + } + p = string(tmp) + } app := v1beta1.Application{ ObjectMeta: metav1.ObjectMeta{ Name: req.Name, Namespace: types.DefaultKubeVelaNS, Annotations: map[string]string{ - types.AnnotationConfigAlias: req.Alias, + types.AnnotationConfigAlias: req.Alias, + types.AnnotationConfigDescription: req.Description, }, Labels: map[string]string{ model.LabelSourceOfTruth: model.FromInner, @@ -153,7 +169,7 @@ func (u *configUseCaseImpl) CreateConfig(ctx context.Context, req apis.CreateCon { Name: req.Name, Type: req.ComponentType, - Properties: &runtime.RawExtension{Raw: []byte(req.Properties)}, + Properties: &runtime.RawExtension{Raw: []byte(p)}, }, }, }, @@ -201,12 +217,7 @@ func (u *configUseCaseImpl) getConfigsByConfigType(ctx context.Context, configTy configs := make([]*apis.Config, len(apps.Items)) for i, a := range apps.Items { - configs[i] = &apis.Config{ - ConfigType: a.Labels[types.LabelConfigType], - Name: a.Name, - Project: a.Labels[types.LabelConfigProject], - CreatedTime: &(a.CreationTimestamp.Time), - } + configs[i] = retrieveConfigFromApplication(a, a.Labels[types.LabelConfigProject]) switch a.Status.Phase { case common.ApplicationRunning: configs[i].Status = configIsReady diff --git a/pkg/apiserver/rest/usecase/config_test.go b/pkg/apiserver/rest/usecase/config_test.go index e3bf34b43..188adea9c 100644 --- a/pkg/apiserver/rest/usecase/config_test.go +++ b/pkg/apiserver/rest/usecase/config_test.go @@ -236,6 +236,11 @@ func TestCreateConfig(t *testing.T) { ctx := context.Background() + properties, err := json.Marshal(map[string]interface{}{ + "name": "default", + }) + assert.NilError(t, err) + testcases := []struct { name string args args @@ -252,6 +257,18 @@ func TestCreateConfig(t *testing.T) { }, }, }, + { + name: "create terraform-alibaba config", + args: args{ + h: h, + req: apis.CreateConfigRequest{ + Name: "n1", + ComponentType: "terraform-alibaba", + Project: "p1", + Properties: string(properties), + }, + }, + }, } for _, tc := range testcases { diff --git a/pkg/apiserver/rest/usecase/project.go b/pkg/apiserver/rest/usecase/project.go index 46f3f3c42..5350e8a03 100644 --- a/pkg/apiserver/rest/usecase/project.go +++ b/pkg/apiserver/rest/usecase/project.go @@ -520,16 +520,10 @@ func (p *projectUsecaseImpl) GetConfigs(ctx context.Context, projectName, config for _, a := range apps.Items { appProject := a.Labels[types.LabelConfigProject] if a.Status.Phase != common.ApplicationRunning || (appProject != "" && appProject != projectName) || - !strings.Contains(a.Labels[types.LabelConfigType], "terraform-") { + !strings.Contains(a.Labels[types.LabelConfigType], types.TerrfaormComponentPrefix) { continue } - configs = append(configs, &apisv1.Config{ - ConfigType: a.Labels[types.LabelConfigType], - Name: a.Name, - Project: appProject, - CreatedTime: &(a.CreationTimestamp.Time), - ApplicationStatus: a.Status.Phase, - }) + configs = append(configs, retrieveConfigFromApplication(a, appProject)) } configs = append(configs, legacyTerraformProviders...) @@ -539,13 +533,7 @@ func (p *projectUsecaseImpl) GetConfigs(ctx context.Context, projectName, config if appProject != "" && appProject != projectName { continue } - configs = append(configs, &apisv1.Config{ - ConfigType: a.Labels[types.LabelConfigType], - Name: a.Name, - Project: appProject, - CreatedTime: &(a.CreationTimestamp.Time), - ApplicationStatus: a.Status.Phase, - }) + configs = append(configs, retrieveConfigFromApplication(a, appProject)) } configs = append(configs, legacyTerraformProviders...) case types.DexConnector, types.HelmRepository, types.ImageRegistry: @@ -556,13 +544,7 @@ func (p *projectUsecaseImpl) GetConfigs(ctx context.Context, projectName, config continue } if a.Labels[types.LabelConfigType] == t { - configs = append(configs, &apisv1.Config{ - ConfigType: a.Labels[types.LabelConfigType], - Name: a.Name, - Project: appProject, - CreatedTime: &(a.CreationTimestamp.Time), - ApplicationStatus: a.Status.Phase, - }) + configs = append(configs, retrieveConfigFromApplication(a, appProject)) } } default: @@ -616,3 +598,15 @@ func ConvertProjectUserModel2Base(user *model.ProjectUser) *apisv1.ProjectUserBa } return base } + +func retrieveConfigFromApplication(a v1beta1.Application, project string) *apisv1.Config { + return &apisv1.Config{ + ConfigType: a.Labels[types.LabelConfigType], + Name: a.Name, + Project: project, + CreatedTime: &(a.CreationTimestamp.Time), + ApplicationStatus: a.Status.Phase, + Alias: a.Annotations[types.AnnotationConfigAlias], + Description: a.Annotations[types.AnnotationConfigDescription], + } +} diff --git a/references/cli/provider.go b/references/cli/provider.go index c48df2c58..a28285511 100644 --- a/references/cli/provider.go +++ b/references/cli/provider.go @@ -193,7 +193,7 @@ func prepareProviderAddSubCommand(c common.Args, ioStreams cmdutil.IOStreams) ([ } data, err := json.Marshal(properties) if err != nil { - return fmt.Errorf("failed to authentiate Terraform cloud provier %s", providerType) + return fmt.Errorf("failed to authenticate Terraform cloud provider %s", providerType) } providerAppName := fmt.Sprintf("config-terraform-provider-%s", name) a := &v1beta1.Application{} @@ -217,12 +217,12 @@ func prepareProviderAddSubCommand(c common.Args, ioStreams cmdutil.IOStreams) ([ }, } if err := k8sClient.Create(ctx, a); err != nil { - return fmt.Errorf("failed to authentiate Terraform cloud provier %s", providerType) + return fmt.Errorf("failed to authenticate Terraform cloud provider %s", providerType) } - ioStreams.Infof("Successfully authentiate provider %s for %s\n", name, providerType) + ioStreams.Infof("Successfully authenticate provider %s for %s\n", name, providerType) return nil } - return fmt.Errorf("failed to authentiate Terraform cloud provier %s", providerType) + return fmt.Errorf("failed to authenticate Terraform cloud provider %s", providerType) } return fmt.Errorf("terraform provider %s for %s already exists", name, providerType) }