Skip to content

Commit

Permalink
Merge pull request #12 from byteplus-sdk/Feat/organization
Browse files Browse the repository at this point in the history
Feat/organization
  • Loading branch information
msq177 authored Oct 31, 2024
2 parents 0ff634d + aaa2c2a commit 2eac092
Show file tree
Hide file tree
Showing 47 changed files with 3,184 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ func DataSourceByteplusCdnDomains() *schema.Resource {
Computed: true,
Description: "Indicates the project to which the domain name belongs.",
},
"tags": bp.TagsSchemaComputed(),
"templates": {
Type: schema.TypeList,
Computed: true,
Expand Down
1 change: 1 addition & 0 deletions byteplus/cdn/cdn_domain/resource_byteplus_cdn_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func ResourceByteplusCdnDomain() *schema.Resource {
Computed: true,
Description: "Indicates the project to which this domain name belongs, with the default value being default.",
},
"tags": bp.TagsSchema(),
"service_region": {
Type: schema.TypeString,
Optional: true,
Expand Down
69 changes: 68 additions & 1 deletion byteplus/cdn/cdn_domain/service_byteplus_cdn_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ func (s *ByteplusCdnDomainService) CreateResource(resourceData *schema.ResourceD
"https_switch": {
TargetField: "HTTPSSwitch",
},
"tags": {
TargetField: "Tags",
ConvertType: bp.ConvertJsonObjectArray,
},
},
ExecuteCall: func(d *schema.ResourceData, client *bp.SdkClient, call bp.SdkCall) (*map[string]interface{}, error) {
logger.Debug(logger.RespFormat, call.Action, call.SdkParam)
Expand Down Expand Up @@ -177,6 +181,8 @@ func (ByteplusCdnDomainService) WithResourceResponseHandlers(d map[string]interf
}

func (s *ByteplusCdnDomainService) ModifyResource(resourceData *schema.ResourceData, resource *schema.Resource) []bp.Callback {
var callbacks []bp.Callback

callback := bp.Callback{
Call: bp.SdkCall{
Action: "UpdateTemplateDomain",
Expand Down Expand Up @@ -210,6 +216,8 @@ func (s *ByteplusCdnDomainService) ModifyResource(resourceData *schema.ResourceD
if d.HasChanges("service_template_id", "service_region",
"https_switch", "cipher_template_id", "cert_id") {
(*call.SdkParam)["Domains"] = []string{d.Id()}

delete(*call.SdkParam, "Tags")
return true, nil
}
return false, nil
Expand All @@ -226,7 +234,12 @@ func (s *ByteplusCdnDomainService) ModifyResource(resourceData *schema.ResourceD
},
},
}
return []bp.Callback{callback}
callbacks = append(callbacks, callback)

// 更新Tags
callbacks = s.setResourceTags(resourceData, "domain", callbacks)

return callbacks
}

func (s *ByteplusCdnDomainService) RemoveResource(resourceData *schema.ResourceData, r *schema.Resource) []bp.Callback {
Expand Down Expand Up @@ -307,6 +320,60 @@ func (s *ByteplusCdnDomainService) ReadResourceId(id string) string {
return id
}

func (s *ByteplusCdnDomainService) setResourceTags(resourceData *schema.ResourceData, resourceType string, callbacks []bp.Callback) []bp.Callback {
addedTags, removedTags, _, _ := bp.GetSetDifference("tags", resourceData, bp.TagsHash, false)

removeCallback := bp.Callback{
Call: bp.SdkCall{
Action: "UntagResources",
ConvertMode: bp.RequestConvertIgnore,
BeforeCall: func(d *schema.ResourceData, client *bp.SdkClient, call bp.SdkCall) (bool, error) {
if removedTags != nil && len(removedTags.List()) > 0 {
(*call.SdkParam)["ResourceIds"] = []string{resourceData.Id()}
(*call.SdkParam)["ResourceType"] = resourceType
(*call.SdkParam)["TagKeys"] = make([]string, 0)
for _, tag := range removedTags.List() {
(*call.SdkParam)["TagKeys"] = append((*call.SdkParam)["TagKeys"].([]string), tag.(map[string]interface{})["key"].(string))
}
return true, nil
}
return false, nil
},
ExecuteCall: func(d *schema.ResourceData, client *bp.SdkClient, call bp.SdkCall) (*map[string]interface{}, error) {
logger.Debug(logger.ReqFormat, call.Action, call.SdkParam)
return s.Client.UniversalClient.DoCall(getUniversalInfo(call.Action), call.SdkParam)
},
},
}
callbacks = append(callbacks, removeCallback)

addCallback := bp.Callback{
Call: bp.SdkCall{
Action: "TagResources",
ConvertMode: bp.RequestConvertIgnore,
BeforeCall: func(d *schema.ResourceData, client *bp.SdkClient, call bp.SdkCall) (bool, error) {
if addedTags != nil && len(addedTags.List()) > 0 {
(*call.SdkParam)["ResourceIds"] = []string{resourceData.Id()}
(*call.SdkParam)["ResourceType"] = resourceType
(*call.SdkParam)["Tags"] = make([]map[string]interface{}, 0)
for _, tag := range addedTags.List() {
(*call.SdkParam)["Tags"] = append((*call.SdkParam)["Tags"].([]map[string]interface{}), tag.(map[string]interface{}))
}
return true, nil
}
return false, nil
},
ExecuteCall: func(d *schema.ResourceData, client *bp.SdkClient, call bp.SdkCall) (*map[string]interface{}, error) {
logger.Debug(logger.ReqFormat, call.Action, call.SdkParam)
return s.Client.UniversalClient.DoCall(getUniversalInfo(call.Action), call.SdkParam)
},
},
}
callbacks = append(callbacks, addCallback)

return callbacks
}

func getUniversalInfo(actionName string) bp.UniversalInfo {
return bp.UniversalInfo{
ServiceName: "CDN",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ func (s *ByteplusCdnDomainService) DatasourceResources(*schema.ResourceData, *sc
},
IdField: "Domain",
CollectField: "domains",
ContentType: bp.ContentTypeJson,
ResponseConverts: map[string]bp.ResponseConvert{
"HTTPS": {
TargetField: "https",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package organization

import (
bp "github.com/byteplus-sdk/terraform-provider-byteplus/common"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
)

func DataSourceByteplusOrganizations() *schema.Resource {
return &schema.Resource{
Read: dataSourceByteplusOrganizationsRead,
Schema: map[string]*schema.Schema{
"name_regex": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringIsValidRegExp,
Description: "A Name Regex of Resource.",
},
"output_file": {
Type: schema.TypeString,
Optional: true,
Description: "File name where to save data source results.",
},
"total_count": {
Type: schema.TypeInt,
Computed: true,
Description: "The total count of query.",
},
"organizations": {
Description: "The collection of query.",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Description: "The id of the organization.",
},
"owner": {
Type: schema.TypeString,
Computed: true,
Description: "The owner id of the organization.",
},
"type": {
Type: schema.TypeInt,
Computed: true,
Description: "The type of the organization.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "The name of the organization.",
},
"description": {
Type: schema.TypeString,
Computed: true,
Description: "The description of the organization.",
},
"status": {
Type: schema.TypeInt,
Computed: true,
Description: "The status of the organization.",
},
"delete_uk": {
Type: schema.TypeString,
Computed: true,
Description: "The delete uk of the organization.",
},
"account_id": {
Type: schema.TypeInt,
Computed: true,
Description: "The account id of the organization owner.",
},
"account_name": {
Type: schema.TypeString,
Computed: true,
Description: "The account name of the organization owner.",
},
"main_name": {
Type: schema.TypeString,
Computed: true,
Description: "The main name of the organization owner.",
},
"created_time": {
Type: schema.TypeString,
Computed: true,
Description: "The created time of the organization.",
},
"updated_time": {
Type: schema.TypeString,
Computed: true,
Description: "The updated time of the organization.",
},
"deleted_time": {
Type: schema.TypeString,
Computed: true,
Description: "The deleted time of the organization.",
},
},
},
},
},
}
}

func dataSourceByteplusOrganizationsRead(d *schema.ResourceData, meta interface{}) error {
service := NewOrganizationService(meta.(*bp.SdkClient))
return service.Dispatcher.Data(service, d, DataSourceByteplusOrganizations())
}
125 changes: 125 additions & 0 deletions byteplus/organization/organization/resource_byteplus_organization.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package organization

import (
"fmt"
"time"

bp "github.com/byteplus-sdk/terraform-provider-byteplus/common"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

/*
Import
Organization can be imported using the id, e.g.
```
$ terraform import byteplus_organization.default resource_id
```
*/

func ResourceByteplusOrganization() *schema.Resource {
resource := &schema.Resource{
Create: resourceByteplusOrganizationCreate,
Read: resourceByteplusOrganizationRead,
Delete: resourceByteplusOrganizationDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(30 * time.Minute),
Delete: schema.DefaultTimeout(30 * time.Minute),
},
Schema: map[string]*schema.Schema{
// computed fields
"owner": {
Type: schema.TypeString,
Computed: true,
Description: "The owner id of the organization.",
},
"type": {
Type: schema.TypeInt,
Computed: true,
Description: "The type of the organization.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "The name of the organization.",
},
"description": {
Type: schema.TypeString,
Computed: true,
Description: "The description of the organization.",
},
"status": {
Type: schema.TypeInt,
Computed: true,
Description: "The status of the organization.",
},
"delete_uk": {
Type: schema.TypeString,
Computed: true,
Description: "The delete uk of the organization.",
},
"account_id": {
Type: schema.TypeInt,
Computed: true,
Description: "The account id of the organization owner.",
},
"account_name": {
Type: schema.TypeString,
Computed: true,
Description: "The account name of the organization owner.",
},
"main_name": {
Type: schema.TypeString,
Computed: true,
Description: "The main name of the organization owner.",
},
"created_time": {
Type: schema.TypeString,
Computed: true,
Description: "The created time of the organization.",
},
"updated_time": {
Type: schema.TypeString,
Computed: true,
Description: "The updated time of the organization.",
},
"deleted_time": {
Type: schema.TypeString,
Computed: true,
Description: "The deleted time of the organization.",
},
},
}
return resource
}

func resourceByteplusOrganizationCreate(d *schema.ResourceData, meta interface{}) (err error) {
service := NewOrganizationService(meta.(*bp.SdkClient))
err = service.Dispatcher.Create(service, d, ResourceByteplusOrganization())
if err != nil {
return fmt.Errorf("error on creating organization %q, %s", d.Id(), err)
}
return resourceByteplusOrganizationRead(d, meta)
}

func resourceByteplusOrganizationRead(d *schema.ResourceData, meta interface{}) (err error) {
service := NewOrganizationService(meta.(*bp.SdkClient))
err = service.Dispatcher.Read(service, d, ResourceByteplusOrganization())
if err != nil {
return fmt.Errorf("error on reading organization %q, %s", d.Id(), err)
}
return err
}

func resourceByteplusOrganizationDelete(d *schema.ResourceData, meta interface{}) (err error) {
service := NewOrganizationService(meta.(*bp.SdkClient))
err = service.Dispatcher.Delete(service, d, ResourceByteplusOrganization())
if err != nil {
return fmt.Errorf("error on deleting organization %q, %s", d.Id(), err)
}
return err
}
Loading

0 comments on commit 2eac092

Please sign in to comment.