Skip to content

Commit

Permalink
Merge pull request #186 from kingsoftcloud/trunk
Browse files Browse the repository at this point in the history
Trunk
  • Loading branch information
notone0010 authored Dec 18, 2023
2 parents 61bbcb6 + c332930 commit 48c5968
Show file tree
Hide file tree
Showing 10 changed files with 654 additions and 3 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@

## 1.13.0 (Dec 18, 2023)

FEATURES:

- - **New Resource:** `ksyun_tag_v2` 标签管理
- - **New Resource:** `ksyun_tag_v2_attachment` 资源绑定标签,支持控制台所有的`resource_type`

## 1.12.1 (Dec 14, 2023)

IMPROVEMENTS:

- `ksyun_healthcheck` 新增`health_check_connect_port`健康检查端口
- `ksyun_alb_listener` 新增`config_content` 个性化配置


## 1.12.0 (Dec 7, 2023)

FEATURES:
Expand Down
6 changes: 6 additions & 0 deletions ksyun/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,8 @@ Tag
Resource
ksyun_tag
ksyun_tag_v2
ksyun_tag_v2_attachment
KS3
Expand Down Expand Up @@ -544,6 +546,10 @@ func Provider() terraform.ResourceProvider {
"ksyun_private_dns_zone": resourceKsyunPrivateDnsZone(),
"ksyun_private_dns_record": resourceKsyunPrivateDnsRecord(),
"ksyun_private_dns_zone_vpc_attachment": resourceKsyunPrivateDnsZoneVpcAttachment(),

// tag
"ksyun_tag_v2": resourceKsyunTagv2(),
"ksyun_tag_v2_attachment": resourceKsyunTagv2Attachment(),
},
ConfigureFunc: providerConfigure,
}
Expand Down
103 changes: 103 additions & 0 deletions ksyun/resource_ksyun_tag_v2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
Provides a Tagv2 resource.
# Example Usage
```hcl
resource "ksyun_tag_v2" "tag" {
key = "test_tag_key"
value = "test_tag_value"
}
```
# Import
Tagv2 can be imported using the `key&value`, e.g.
```
$ terraform import ksyun_tag_v2.tag ${tagv2_key}:${tagv2_value}
```
*/

package ksyun

import (
"fmt"

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

func resourceKsyunTagv2() *schema.Resource {
return &schema.Resource{
Create: resourceKsyunTagv2Create,
Read: resourceKsyunTagv2Read,
Update: resourceKsyunTagv2Update,
Delete: resourceKsyunTagv2Delete,
Importer: &schema.ResourceImporter{
State: importTagResource,
},
CustomizeDiff: resourceKsyunTagv2Diff(),
Schema: map[string]*schema.Schema{
"key": {
Type: schema.TypeString,
Required: true,
Description: "Tag key.",
},
"value": {
Type: schema.TypeString,
Required: true,
Description: "Tag value.",
},
},
}
}

func resourceKsyunTagv2Diff() schema.CustomizeDiffFunc {
return func(diff *schema.ResourceDiff, i interface{}) (err error) {
keys := []string{"key", "value", "resource_type", "resource_id"}

for _, k := range keys {
if diff.HasChange(k) {
err = diff.ForceNew(k)
if err != nil {
return
}
}
}

return
}
}

func resourceKsyunTagv2Create(d *schema.ResourceData, meta interface{}) (err error) {
tagService := TagService{meta.(*KsyunClient)}
err = tagService.CreateTag(d, resourceKsyunTagv2())
if err != nil {
return fmt.Errorf("error on creating Tagv2 %q, %s", d.Id(), err)
}
return resourceKsyunTagv2Read(d, meta)
}

func resourceKsyunTagv2Update(d *schema.ResourceData, meta interface{}) (err error) {
return
}

func resourceKsyunTagv2Read(d *schema.ResourceData, meta interface{}) (err error) {
tagService := TagService{meta.(*KsyunClient)}
err = tagService.ReadAndSetTag(d, resourceKsyunTagv2())
if err != nil {
return fmt.Errorf("error on reading Tagv2, %s", err)
}
return
}

func resourceKsyunTagv2Delete(d *schema.ResourceData, meta interface{}) (err error) {
tagService := TagService{meta.(*KsyunClient)}
err = tagService.DeleteTag(d)
if err != nil {
return fmt.Errorf("error on deleting Tagv2 %q, %s", d.Id(), err)
}
return
}
129 changes: 129 additions & 0 deletions ksyun/resource_ksyun_tag_v2_attachment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
Provides an attachment for pinning tag upon resource.
> Note: supported all of resource_type
> The tag will be created if it is not existed.
# Example Usage
```hcl
resource "ksyun_tag_v2" "tagv2" {
key = "test_tag_key"
value = "test_tag_value"
}
resource "ksyun_tag_v2_attachment" "tag" {
key = "test_tag_key"
value = "test_tag_value"
resource_type = "redis-instance"
resource_id = "1f4e8c22-xxxx-xxxx-xxxx-cc6345011af4"
}
```
# Import
Tagv2Attachment can be imported using the `id`, e.g.
```
$ terraform import ksyun_tag_v2_attachment.tag ${tag_key}:${tag_value},${resource_type}:${resource_id}
```
*/

package ksyun

import (
"fmt"

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

func resourceKsyunTagv2Attachment() *schema.Resource {
return &schema.Resource{
Create: resourceKsyunTagv2AttachmentCreate,
Read: resourceKsyunTagv2AttachmentRead,
Update: resourceKsyunTagv2AttachmentUpdate,
Delete: resourceKsyunTagv2AttachmentDelete,
Importer: &schema.ResourceImporter{
State: importTagV1Resource,
},
CustomizeDiff: resourceKsyunTagv2AttachmentDiff(),
Schema: map[string]*schema.Schema{
"key": {
Type: schema.TypeString,
Required: true,
Description: "Tag key.",
},
"value": {
Type: schema.TypeString,
Required: true,
Description: "Tag value.",
},
"resource_type": {
Type: schema.TypeString,
Required: true,
Description: "Resource type. [supported type](https://docs.ksyun.com/documents/39807).",
},
"resource_id": {
Type: schema.TypeString,
Required: true,
Description: "Resource ID.",
},

"tag_id": {
Type: schema.TypeFloat,
Computed: true,
Description: "Tag id.",
},
},
}
}

func resourceKsyunTagv2AttachmentDiff() schema.CustomizeDiffFunc {
return func(diff *schema.ResourceDiff, i interface{}) (err error) {
keys := []string{"key", "value", "resource_type", "resource_id"}

for _, k := range keys {
if diff.HasChange(k) {
err = diff.ForceNew(k)
if err != nil {
return
}
}
}

return
}
}

func resourceKsyunTagv2AttachmentCreate(d *schema.ResourceData, meta interface{}) (err error) {
tagService := TagService{meta.(*KsyunClient)}
err = tagService.CreateTagResourceAttachment(d, resourceKsyunTagv2Attachment())
if err != nil {
return fmt.Errorf("error on creating Tagv2Attachment %q, %s", d.Id(), err)
}
return resourceKsyunTagv2AttachmentRead(d, meta)
}

func resourceKsyunTagv2AttachmentUpdate(d *schema.ResourceData, meta interface{}) (err error) {
return
}

func resourceKsyunTagv2AttachmentRead(d *schema.ResourceData, meta interface{}) (err error) {
tagService := TagService{meta.(*KsyunClient)}
err = tagService.ReadAndSetTagAttachment(d, resourceKsyunTagv2Attachment())
if err != nil {
return fmt.Errorf("error on reading Tagv2Attachment, %s", err)
}
return
}

func resourceKsyunTagv2AttachmentDelete(d *schema.ResourceData, meta interface{}) (err error) {
tagService := TagService{meta.(*KsyunClient)}
err = tagService.DetachResourceTags(d)
if err != nil {
return fmt.Errorf("error on deleting Tagv2Attachment %q, %s", d.Id(), err)
}
return
}
Loading

0 comments on commit 48c5968

Please sign in to comment.