Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(CodeArts/Deploy): support to copy application #6102

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions docs/resources/codearts_deploy_application_copy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
subcategory: "CodeArts Deploy"
layout: "huaweicloud"
page_title: "HuaweiCloud: huaweicloud_codearts_deploy_application_copy"
description: |-
Manages a CodeArts deploy application copy resource within HuaweiCloud.
---

# huaweicloud_codearts_deploy_application_copy

Manages a CodeArts deploy application copy resource within HuaweiCloud.

## Example Usage

```hcl
variable "app_id" {}

resource "huaweicloud_codearts_deploy_application_copy" "test" {
app_id = var.app_id
}
```

## Argument Reference

The following arguments are supported:

* `region` - (Optional, String, ForceNew) Specifies the region in which to create the resource.
If omitted, the provider-level region will be used.
Changing this creates a new resource.

* `app_id` - (Required, String, ForceNew) Specifies the application ID.
Changing this creates a new resource.

## Attribute Reference

In addition to all arguments above, the following attributes are exported:

* `id` - The resource ID, is same as the new application ID.
1 change: 1 addition & 0 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2222,6 +2222,7 @@ func Provider() *schema.Provider {
"huaweicloud_codearts_repository": codearts.ResourceRepository(),

"huaweicloud_codearts_deploy_application": codeartsdeploy.ResourceDeployApplication(),
"huaweicloud_codearts_deploy_application_copy": codeartsdeploy.ResourceDeployApplicationCopy(),
"huaweicloud_codearts_deploy_application_group": codeartsdeploy.ResourceDeployApplicationGroup(),
"huaweicloud_codearts_deploy_application_group_move": codeartsdeploy.ResourceDeployApplicationGroupMove(),
"huaweicloud_codearts_deploy_group": codeartsdeploy.ResourceDeployGroup(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package codeartsdeploy

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"

"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance"
)

func TestAccCodeArtsDeployApplicationCopy_basic(t *testing.T) {
name := acceptance.RandomAccResourceName()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.TestAccPreCheck(t) },
ProviderFactories: acceptance.TestAccProviderFactories,
CheckDestroy: nil,
Steps: []resource.TestStep{
{
Config: testCodeArtsDeployApplicationCopy_basic(name),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckOutput("applications_len_before_copy", "true"),
resource.TestCheckOutput("applications_len_after_copy", "true"),
),
},
},
})
}

func testCodeArtsDeployApplicationCopy_basic(name string) string {
return fmt.Sprintf(`
%[1]s

data "huaweicloud_codearts_deploy_applications" "beforeCopy" {
depends_on = [huaweicloud_codearts_deploy_application.test]

project_id = huaweicloud_codearts_project.test.id
}

resource "huaweicloud_codearts_deploy_application_copy" "test" {
app_id = huaweicloud_codearts_deploy_application.test.id
}

data "huaweicloud_codearts_deploy_applications" "afterCopy" {
depends_on = [huaweicloud_codearts_deploy_application_copy.test]

project_id = huaweicloud_codearts_project.test.id
}

output "applications_len_before_copy" {
value = length(data.huaweicloud_codearts_deploy_applications.beforeCopy.applications) == 1
}

output "applications_len_after_copy" {
value = length(data.huaweicloud_codearts_deploy_applications.afterCopy.applications) == 2
}
`, testDeployApplication_basic(name))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package codeartsdeploy

import (
"context"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/chnsz/golangsdk"

"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
)

// @API CodeArtsDeploy POST /v1/applications/{app_id}/duplicate
func ResourceDeployApplicationCopy() *schema.Resource {
return &schema.Resource{
CreateContext: resourceCodeArtsDeployApplicationCopyCreate,
ReadContext: resourceCodeArtsDeployApplicationCopyRead,
DeleteContext: resourceCodeArtsDeployApplicationCopyDelete,

Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"app_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `Specifies the application ID.`,
},
},
}
}

func resourceCodeArtsDeployApplicationCopyCreate(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)
client, err := cfg.NewServiceClient("codearts_deploy", region)
if err != nil {
return diag.Errorf("error creating CodeArts deploy client: %s", err)
}

httpUrl := "v1/applications/{app_id}/duplicate"
createPath := client.Endpoint + httpUrl
createPath = strings.ReplaceAll(createPath, "{app_id}", d.Get("app_id").(string))
createOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
}

createResp, err := client.Request("POST", createPath, &createOpt)
if err != nil {
return diag.Errorf("error copying CodeArts deploy application: %s", err)
}

createRespBody, err := utils.FlattenResponse(createResp)
if err != nil {
return diag.FromErr(err)
}

copyId := utils.PathSearch("result.id", createRespBody, "").(string)
if copyId == "" {
return diag.Errorf("unable to find the CodeArts deploy new application ID from the API response")
}

d.SetId(copyId)

return nil
}

func resourceCodeArtsDeployApplicationCopyRead(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics {
return nil
}

func resourceCodeArtsDeployApplicationCopyDelete(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics {
errorMsg := "Deleting application copy resource is not supported. The resource is only removed from the" +
"state, the application remain in the cloud."
return diag.Diagnostics{
diag.Diagnostic{
Severity: diag.Warning,
Summary: errorMsg,
},
}
}
Loading