Skip to content

Commit

Permalink
feat(ges): import ges_metadata resource and doc
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhukun-Huawei committed Dec 28, 2023
1 parent 85f4c3f commit 14c021d
Show file tree
Hide file tree
Showing 3 changed files with 315 additions and 0 deletions.
136 changes: 136 additions & 0 deletions docs/resources/ges_metadata.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
---
subcategory: "Graph Engine Service (GES)"
---

# g42cloud_ges_metadata

Manages a GES metadata resource within G42Cloud.

-> Only 50 metadata resources can be created.

## Example Usage

```hcl
variable "obs_path" {}
resource "g42cloud_ges_metadata" "test" {
name = "demo"
description = "This is a demo"
metadata_path = var.obs_path
ges_metadata {
labels {
name = "user"
properties = [{
"dataType" = "char"
"name" = "sex"
"cardinality" = "single"
},
{
"dataType" = "enum"
"name" = "country"
"cardinality" = "single"
"typeNameCount" = "3"
"typeName1" = "US"
"typeName2" = "EN"
"typeName3" = "CN"
},
{
"dataType" = "char array"
"name" = "firstName"
"cardinality" = "single"
"maxDataSize" = "20"
},
{
"dataType" = "string"
"name" = "lastName"
"cardinality" = "single"
},
{
"dataType" = "long"
"name" = "children"
"cardinality" = "set"
},
{
"dataType" = "long"
"name" = "friends"
"cardinality" = "list"
},
{
"dataType" = "enum"
"name" = "cards"
"cardinality" = "list"
"typeNameCount" = "3"
"typeName1" = "card_1"
"typeName2" = "card_2"
"typeName3" = "card_3"
}
]
}
}
}
```

## 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 parameter will create a new resource.

* `name` - (Required, String, ForceNew) Metadata name.
The name contains 1 to 64 characters consisting of only letters, digits, and underscores (_).
Changing this parameter will create a new resource.

* `metadata_path` - (Required, String) OBS Path for storing the metadata.

* `description` - (Required, String, ForceNew) Metadata description.

Changing this parameter will create a new resource.

* `ges_metadata` - (Required, List) Object for storing metadata message information.
The [ges_metadata](#GesMetadata_Metadata) structure is documented below.

* `encryption` - (Optional, List) The configuration of data encryption.
The graph instance is not encrypted by default.
The [encryption](#GesMetadata_Encryption) structure is documented below.

<a name="GesMetadata_Metadata"></a>
The `ges_metadata` block supports:

* `labels` - (Optional, List) Label list.
For details, see [data formats](https://docs.g42cloud.com/usermanual/ges/ges_01_0153.html).
The [labels](#GesMetadata_MetadataLabels) structure is documented below.

<a name="GesMetadata_MetadataLabels"></a>
The `labels` block supports:

* `name` - (Optional, String) Name of a label.

* `properties` - (Optional, List) The list of label properties. A property refers to the data format of a single
property and contains some fields.
For details, see [data formats](https://docs.g42cloud.com/usermanual/ges/ges_01_0153.html).

<a name="GesMetadata_Encryption"></a>
The `encryption` block supports:

* `enable` - (Optional, Bool) Whether to enable data encryption The value can be true or false.
The default value is false.

* `master_key_id` - (Optional, String) ID of the customer master key created by DEW in the project where
the graph is created.

## Attribute Reference

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

* `id` - The resource ID.

* `status` - Status of a metadata. **200** is available.

## Import

The ges metadata can be imported using the `id`, e.g.

```bash
terraform import g42cloud_ges_metadata.test 55b32ad9-1aba-407d-86cf-85f4f765d37a
```
11 changes: 11 additions & 0 deletions g42cloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package g42cloud

import (
"fmt"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/ges"
"log"
"strings"
"sync"
Expand Down Expand Up @@ -446,8 +447,18 @@ func Provider() *schema.Provider {

"g42cloud_fgs_function": fgs.ResourceFgsFunctionV2(),

"g42cloud_ges_metadata": ges.ResourceGesMetadata(),

"g42cloud_identity_role_assignment": iam.ResourceIdentityGroupRoleAssignment(),
"g42cloud_identity_user": iam.ResourceIdentityUser(),
"g42cloud_identity_group": iam.ResourceIdentityGroup(),
"g42cloud_identity_group_membership": iam.ResourceIdentityGroupMembership(),
"g42cloud_identity_acl": iam.ResourceIdentityACL(),
"g42cloud_identity_agency": iam.ResourceIAMAgencyV3(),
"g42cloud_identity_project": iam.ResourceIdentityProject(),
"g42cloud_identity_provider": iam.ResourceIdentityProvider(),
"g42cloud_identity_provider_conversion": iam.ResourceIAMProviderConversion(),
"g42cloud_identity_role": iam.ResourceIdentityRole(),

"g42cloud_images_image": ims.ResourceImsImage(),

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
package ges

import (
"fmt"
"strings"
"testing"

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

"github.com/chnsz/golangsdk"

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

func getGesMetadataResourceFunc(cfg *config.Config, state *terraform.ResourceState) (interface{}, error) {
region := acceptance.G42_REGION_NAME
// getMetadataDetail: Query the GES metadata detail.
var (
getMetadataDetailHttpUrl = "v1.0/{project_id}/graphs/metadatas/{id}"
getMetadataDetailProduct = "ges"
)
getMetadataDetailClient, err := cfg.NewServiceClient(getMetadataDetailProduct, region)
if err != nil {
return nil, fmt.Errorf("error creating GES Client: %s", err)
}

getMetadataDetailPath := getMetadataDetailClient.Endpoint + getMetadataDetailHttpUrl
getMetadataDetailPath = strings.ReplaceAll(getMetadataDetailPath, "{project_id}", getMetadataDetailClient.ProjectID)
getMetadataDetailPath = strings.ReplaceAll(getMetadataDetailPath, "{id}", state.Primary.ID)

getMetadataDetailOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
OkCodes: []int{
200,
},
MoreHeaders: map[string]string{"Content-Type": "application/json;charset=UTF-8"},
}

getMetadataDetailResp, err := getMetadataDetailClient.Request("GET", getMetadataDetailPath, &getMetadataDetailOpt)
if err != nil {
return nil, fmt.Errorf("error retrieving GES metadata: %s", err)
}

getMetadataDetailRespBody, err := utils.FlattenResponse(getMetadataDetailResp)
if err != nil {
return nil, fmt.Errorf("error retrieving GES metadata: %s", err)
}

return getMetadataDetailRespBody, nil
}

func TestAccGesMetadata_basic(t *testing.T) {
var obj interface{}

name := acceptance.RandomAccResourceName()
rName := "g42cloud_ges_metadata.test"
bucketName := acceptance.RandomAccResourceNameWithDash()

rc := acceptance.InitResourceCheck(
rName,
&obj,
getGesMetadataResourceFunc,
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.TestAccPreCheck(t) },
ProviderFactories: acceptance.TestAccProviderFactories,
CheckDestroy: rc.CheckResourceDestroy(),
Steps: []resource.TestStep{
{
Config: testGesMetadata_basic(name, bucketName),
Check: resource.ComposeTestCheckFunc(
rc.CheckResourceExists(),
resource.TestCheckResourceAttr(rName, "name", name),
resource.TestCheckResourceAttr(rName, "description", "This is a demo"),
resource.TestCheckResourceAttr(rName, "ges_metadata.0.labels.0.properties.#", "1"),
resource.TestCheckResourceAttrSet(rName, "status"),
),
},
{
Config: testGesMetadata_basic_update(name, bucketName),
Check: resource.ComposeTestCheckFunc(
rc.CheckResourceExists(),
resource.TestCheckResourceAttr(rName, "name", name),
resource.TestCheckResourceAttr(rName, "description", "This is a demo"),
resource.TestCheckResourceAttr(rName, "ges_metadata.0.labels.0.properties.#", "2"),
),
},
{
ResourceName: rName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testGesMetadata_basic(name, bucketName string) string {
return fmt.Sprintf(`
resource "g42cloud_obs_bucket" "test" {
bucket = "%s"
acl = "private"
force_destroy = true
}
resource "g42cloud_ges_metadata" "test" {
name = "%s"
description = "This is a demo"
metadata_path = "${g42cloud_obs_bucket.test.bucket}/schema_%s.xml"
ges_metadata {
labels {
name = "user"
properties = [{
"dataType" = "char"
"name" = "sex"
"cardinality" = "single"
}]
}
}
depends_on = [
g42cloud_obs_bucket.test
]
}
`, bucketName, name, name)
}

func testGesMetadata_basic_update(name, bucketName string) string {
return fmt.Sprintf(`
resource "g42cloud_obs_bucket" "test" {
bucket = "%s"
acl = "private"
force_destroy = true
}
resource "g42cloud_ges_metadata" "test" {
name = "%s"
description = "This is a demo"
metadata_path = "${g42cloud_obs_bucket.test.bucket}/schema_%s.xml"
ges_metadata {
labels {
name = "user"
properties = [{
"dataType" = "char"
"name" = "sex"
"cardinality" = "single"
},
{
"dataType" = "enum"
"name" = "country"
"cardinality" = "single"
"typeNameCount" = "3"
"typeName1" = "US"
"typeName2" = "EN"
"typeName3" = "CN"
}]
}
}
depends_on = [
g42cloud_obs_bucket.test
]
}
`, bucketName, name, name)
}

0 comments on commit 14c021d

Please sign in to comment.