Skip to content

Commit

Permalink
feat(ccm): ccm support a new datasource to export private CA (#5285)
Browse files Browse the repository at this point in the history
  • Loading branch information
deer-hang authored Jul 26, 2024
1 parent 4233d19 commit 70384c9
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 0 deletions.
42 changes: 42 additions & 0 deletions docs/data-sources/ccm_private_ca_export.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
subcategory: "Cloud Certificate Manager (CCM)"
layout: "huaweicloud"
page_title: "HuaweiCloud: huaweicloud_ccm_private_ca_export"
description: |-
Use this data source to export a private CA within HuaweiCloud.
---

# huaweicloud_ccm_private_ca_export

Use this data source to export a private CA within HuaweiCloud.

-> Only CAs in `ACTIVED`, `DISABLED` or `EXPIRED` status support exporting operation.

## Example Usage

```hcl
variable "ca_id" {}
data "huaweicloud_ccm_private_ca_export" "test" {
ca_id = var.ca_id
}
```

## Argument Reference

The following arguments are supported:

* `region` - (Optional, String) Specifies the region in which to query the resource.
If omitted, the provider-level region will be used.

* `ca_id` - (Required, String) Specifies the ID of the CA you want to export.

## Attribute Reference

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

* `id` - The data source ID.

* `certificate` - The certificate content.

* `certificate_chain` - The content of the certificate chain.
1 change: 1 addition & 0 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,7 @@ func Provider() *schema.Provider {
"huaweicloud_ccm_certificates": ccm.DataSourceCertificates(),
"huaweicloud_ccm_certificate_export": ccm.DataSourceCertificateExport(),
"huaweicloud_ccm_private_cas": ccm.DataSourcePrivateCas(),
"huaweicloud_ccm_private_ca_export": ccm.DataSourcePrivateCaExport(),
"huaweicloud_ccm_private_certificates": ccm.DataSourcePrivateCertificates(),
"huaweicloud_ccm_private_certificate_export": ccm.DataSourceCcmPrivateCertificateExport(),

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package ccm

import (
"fmt"
"testing"

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

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

func TestAccDataSourcePrivateCaExport_basic(t *testing.T) {
var (
dataSource = "data.huaweicloud_ccm_private_ca_export.test"
rName = acceptance.RandomAccResourceName()
dc = acceptance.InitDataSourceCheck(dataSource)
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acceptance.TestAccPreCheck(t)
},
ProviderFactories: acceptance.TestAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testDataSourceDataSourcePrivateCaExport_basic(rName),
Check: resource.ComposeTestCheckFunc(
dc.CheckResourceExists(),
resource.TestCheckResourceAttrSet(dataSource, "certificate"),
),
},
},
})
}

func testDataSourceDataSourcePrivateCaExport_basic(name string) string {
return fmt.Sprintf(`
%s
data "huaweicloud_ccm_private_ca_export" "test" {
ca_id = huaweicloud_ccm_private_ca.test_root.id
}
`, tesPrivateCA_base(name))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Generated by PMS #277
package ccm

import (
"context"
"strings"

"github.com/hashicorp/go-multierror"
"github.com/hashicorp/go-uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/tidwall/gjson"

"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/httphelper"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/schemas"
)

func DataSourcePrivateCaExport() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourcePrivateCaExportRead,

Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: `Specifies the region in which to query the resource. If omitted, the provider-level region will be used.`,
},
"ca_id": {
Type: schema.TypeString,
Required: true,
Description: `Specifies the ID of the CA certificate you want to export.`,
},
"certificate": {
Type: schema.TypeString,
Computed: true,
Description: `The certificate content.`,
},
"certificate_chain": {
Type: schema.TypeString,
Computed: true,
Description: `The content of the certificate chain.`,
},
},
}
}

type PrivateCaExportDSWrapper struct {
*schemas.ResourceDataWrapper
Config *config.Config
}

func newPrivateCaExportDSWrapper(d *schema.ResourceData, meta interface{}) *PrivateCaExportDSWrapper {
return &PrivateCaExportDSWrapper{
ResourceDataWrapper: schemas.NewSchemaWrapper(d),
Config: meta.(*config.Config),
}
}

func dataSourcePrivateCaExportRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
wrapper := newPrivateCaExportDSWrapper(d, meta)
expCerAutCerRst, err := wrapper.ExportCertificateAuthorityCertificate()
if err != nil {
return diag.FromErr(err)
}

id, err := uuid.GenerateUUID()
if err != nil {
return diag.FromErr(err)
}
d.SetId(id)

err = wrapper.exportCertificateAuthorityCertificateToSchema(expCerAutCerRst)
if err != nil {
return diag.FromErr(err)
}

return nil
}

// @API CCM POST /v1/private-certificate-authorities/{ca_id}/export
func (w *PrivateCaExportDSWrapper) ExportCertificateAuthorityCertificate() (*gjson.Result, error) {
client, err := w.NewClient(w.Config, "ccm")
if err != nil {
return nil, err
}

uri := "/v1/private-certificate-authorities/{ca_id}/export"
uri = strings.ReplaceAll(uri, "{ca_id}", w.Get("ca_id").(string))
return httphelper.New(client).
Method("POST").
URI(uri).
Request().
Result()
}

func (w *PrivateCaExportDSWrapper) exportCertificateAuthorityCertificateToSchema(body *gjson.Result) error {
d := w.ResourceData
mErr := multierror.Append(nil,
d.Set("region", w.Config.GetRegion(w.ResourceData)),
d.Set("certificate", body.Get("certificate").Value()),
d.Set("certificate_chain", body.Get("certificate_chain").Value()),
)
return mErr.ErrorOrNil()
}

0 comments on commit 70384c9

Please sign in to comment.