Skip to content

Commit

Permalink
Goverance: Improves the invoking goverance api method and supports re…
Browse files Browse the repository at this point in the history
…freshing credential automatically
  • Loading branch information
xiaozhu36 committed Dec 20, 2024
1 parent c3fbf81 commit c71cfe3
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 60 deletions.
10 changes: 2 additions & 8 deletions alicloud/data_source_alicloud_governance_baselines.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"time"

"github.com/PaesslerAG/jsonpath"
util "github.com/alibabacloud-go/tea-utils/service"
"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -71,18 +70,13 @@ func dataSourceAliCloudGovernanceBaselineRead(d *schema.ResourceData, meta inter
var response map[string]interface{}
var query map[string]interface{}
action := "ListAccountFactoryBaselines"
conn, err := client.NewGovernanceClient()
if err != nil {
return WrapError(err)
}
var err error
request = make(map[string]interface{})
query = make(map[string]interface{})
query["RegionId"] = client.RegionId
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 5*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2021-01-20"), StringPointer("AK"), query, request, &runtime)
response, err = client.RpcPost("governance", "2021-01-20", action, query, request, true)

if err != nil {
if NeedRetry(err) {
Expand Down
19 changes: 4 additions & 15 deletions alicloud/resource_alicloud_governance_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"time"

"github.com/PaesslerAG/jsonpath"
util "github.com/alibabacloud-go/tea-utils/service"
"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -93,10 +92,7 @@ func resourceAliCloudGovernanceAccountCreate(d *schema.ResourceData, meta interf
var request map[string]interface{}
var response map[string]interface{}
query := make(map[string]interface{})
conn, err := client.NewGovernanceClient()
if err != nil {
return WrapError(err)
}
var err error
request = make(map[string]interface{})
if v, ok := d.GetOk("account_id"); ok {
request["AccountUid"] = v
Expand Down Expand Up @@ -143,11 +139,9 @@ func resourceAliCloudGovernanceAccountCreate(d *schema.ResourceData, meta interf
baselineItemsMaps = append(baselineItemsMaps, baselineItem)
}
request["BaselineItems"] = baselineItemsMaps
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 5*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2021-01-20"), StringPointer("AK"), query, request, &runtime)
response, err = client.RpcPost("governance", "2021-01-20", action, query, request, false)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down Expand Up @@ -229,10 +223,7 @@ func resourceAliCloudGovernanceAccountUpdate(d *schema.ResourceData, meta interf
update := false

action := "EnrollAccount"
conn, err := client.NewGovernanceClient()
if err != nil {
return WrapError(err)
}
var err error
request = make(map[string]interface{})
query = make(map[string]interface{})
request["AccountUid"] = d.Id()
Expand Down Expand Up @@ -262,11 +253,9 @@ func resourceAliCloudGovernanceAccountUpdate(d *schema.ResourceData, meta interf
request["BaselineId"] = d.Get("baseline_id")

if update {
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 5*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2021-01-20"), StringPointer("AK"), query, request, &runtime)
response, err = client.RpcPost("governance", "2021-01-20", action, query, request, false)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down
28 changes: 6 additions & 22 deletions alicloud/resource_alicloud_governance_baseline.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"log"
"time"

util "github.com/alibabacloud-go/tea-utils/service"
"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -69,10 +68,7 @@ func resourceAliCloudGovernanceBaselineCreate(d *schema.ResourceData, meta inter
var request map[string]interface{}
var response map[string]interface{}
query := make(map[string]interface{})
conn, err := client.NewGovernanceClient()
if err != nil {
return WrapError(err)
}
var err error
request = make(map[string]interface{})
query["RegionId"] = client.RegionId

Expand All @@ -95,11 +91,9 @@ func resourceAliCloudGovernanceBaselineCreate(d *schema.ResourceData, meta inter
if v, ok := d.GetOk("description"); ok {
request["Description"] = v
}
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 5*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2021-01-20"), StringPointer("AK"), query, request, &runtime)
response, err = client.RpcPost("governance", "2021-01-20", action, query, request, false)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down Expand Up @@ -170,10 +164,7 @@ func resourceAliCloudGovernanceBaselineUpdate(d *schema.ResourceData, meta inter
var query map[string]interface{}
update := false
action := "UpdateAccountFactoryBaseline"
conn, err := client.NewGovernanceClient()
if err != nil {
return WrapError(err)
}
var err error
request = make(map[string]interface{})
query = make(map[string]interface{})
query["BaselineId"] = d.Id()
Expand Down Expand Up @@ -205,11 +196,9 @@ func resourceAliCloudGovernanceBaselineUpdate(d *schema.ResourceData, meta inter
}

if update {
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 5*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2021-01-20"), StringPointer("AK"), query, request, &runtime)
response, err = client.RpcPost("governance", "2021-01-20", action, query, request, false)
if err != nil {
if NeedRetry(err) {
wait()
Expand All @@ -235,19 +224,14 @@ func resourceAliCloudGovernanceBaselineDelete(d *schema.ResourceData, meta inter
var request map[string]interface{}
var response map[string]interface{}
query := make(map[string]interface{})
conn, err := client.NewGovernanceClient()
if err != nil {
return WrapError(err)
}
var err error
request = make(map[string]interface{})
query["BaselineId"] = d.Id()
query["RegionId"] = client.RegionId

runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 5*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutDelete), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2021-01-20"), StringPointer("AK"), query, request, &runtime)
response, err = client.RpcPost("governance", "2021-01-20", action, query, request, false)

if err != nil {
if NeedRetry(err) {
Expand Down
17 changes: 2 additions & 15 deletions alicloud/service_alicloud_governance_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"time"

"github.com/PaesslerAG/jsonpath"
util "github.com/alibabacloud-go/tea-utils/service"
"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)
Expand All @@ -23,20 +22,14 @@ func (s *GovernanceServiceV2) DescribeGovernanceBaseline(id string) (object map[
var response map[string]interface{}
var query map[string]interface{}
action := "GetAccountFactoryBaseline"
conn, err := client.NewGovernanceClient()
if err != nil {
return object, WrapError(err)
}
request = make(map[string]interface{})
query = make(map[string]interface{})
query["BaselineId"] = id
query["RegionId"] = client.RegionId

runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 5*time.Second)
err = resource.Retry(1*time.Minute, func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2021-01-20"), StringPointer("AK"), query, request, &runtime)
response, err = client.RpcPost("governance", "2021-01-20", action, query, request, true)

if err != nil {
if NeedRetry(err) {
Expand Down Expand Up @@ -91,20 +84,14 @@ func (s *GovernanceServiceV2) DescribeGovernanceAccount(id string) (object map[s
var response map[string]interface{}
var query map[string]interface{}
action := "GetEnrolledAccount"
conn, err := client.NewGovernanceClient()
if err != nil {
return object, WrapError(err)
}
request = make(map[string]interface{})
query = make(map[string]interface{})
request["AccountUid"] = id
request["RegionId"] = client.RegionId

runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 5*time.Second)
err = resource.Retry(1*time.Minute, func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2021-01-20"), StringPointer("AK"), query, request, &runtime)
response, err = client.RpcPost("governance", "2021-01-20", action, query, request, true)

if err != nil {
if NeedRetry(err) {
Expand Down
61 changes: 61 additions & 0 deletions demo_provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
terraform {
required_providers {
alicloud = {
source = "hashicorp/alicloud"
version = "1.0.0"
# version = "1.232.0"
}
# aws = {
# source = "hashicorp/aws"
# version = "5.17.0"
# }
}
}

variable "region" {
default = "cn-hangzhou"
}

variable "profile_name" {
default = "quanxi"
}
provider "alicloud" {
profile = var.profile_name
region = var.region
}

provider "alicloud" {
profile = var.profile_name
alias = "hz"
region = "cn-hangzhou"
}

provider "alicloud" {
alias = "assume"
region = "cn-qingdao"
# profile = "quanxi"
# profile = "assumeRole"
profile = "ak-for-assumerole"
# region = ""
# configuration_source = "xiaozhu-demo-for-assumerole"
# source_ip = "10.1.1.1"
assume_role {
role_arn = "acs:ram::1182725234319447:role/assumeroletest"
session_expiration = 900
# external_id = "terraformTestExternalId"
# "Condition": {
# "StringEquals": {
# "sts:ExternalId": "terraformTestExternalId"
# }
# },
}
# assume_role_with_oidc {
# oidc_provider_arn = "acs:ram::1182725234319447:oidc-provider/ack-rrsa-c3470446df7a64cee9c6bf5ec949ea2ec"
# role_arn = "acs:ram::1182725234319447:role/demo-role-for-rrsa"
## oidc_token_file = "./rrsa-token"
## role_session_name = "terraform"
# oidc_token = "eyJhbGciOiJSUzI1NiIsImtpZCI6IjE5UjRIbWNPekhEVGtJbk9iUVNFcXVQSGw3bURuNG4tLUxhOVZ4VkhUd1EifQ.eyJhdWQiOlsic3RzLmFsaXl1bmNzLmNvbSJdLCJleHAiOjE3MjMwNDIxMzcsImlhdCI6MTcyMzAzODUzNywiaXNzIjoiaHR0cHM6Ly9vaWRjLWFjay1jbi1iZWlqaW5nLm9zcy1jbi1iZWlqaW5nLmFsaXl1bmNzLmNvbS9jMzQ3MDQ0NmRmN2E2NGNlZTljNmJmNWVjOTQ5ZWEyZWMiLCJrdWJlcm5ldGVzLmlvIjp7Im5hbWVzcGFjZSI6InJyc2EtZGVtbyIsInBvZCI6eyJuYW1lIjoiZGVtbyIsInVpZCI6IjA5MjUwNmJkLTdjMjgtNGVjMS1hYjk3LTQyNmU1NTQ3OWRhNSJ9LCJzZXJ2aWNlYWNjb3VudCI6eyJuYW1lIjoiZGVtby1zYSIsInVpZCI6IjczNDRiOWZmLWFjMDktNGQyMy1hMjQ5LTJlNjE5NWJlY2UxZSJ9fSwibmJmIjoxNzIzMDM4NTM3LCJzdWIiOiJzeXN0ZW06c2VydmljZWFjY291bnQ6cnJzYS1kZW1vOmRlbW8tc2EifQ.KRgbCUFhUx27jSjG885ZjECtCoHngccI77JMDwlV_t8O7-40WjHe0LSqTO0eqDDJEztcD63q8kCY5gvimHAd2bXrQ4TrS7P4Y1N095quBAegsQB9tlDU680pSVVUROGfJ30ZYs24q6FZXcFGlCH60TIFc6qgGnbOwPJeJeuio0vExGEtDzvqXN6498v1ifzT72yWpcTPlBFtxueLkAzx8-9OS7SL7jhv9czsrxopn5CC-tHCuZ-fSaCs7PtYqT8kUws9fNM2pvATikpfXDlxNI6vSipN2BN9DYn6YN4YdmFF4PG6P8lDaEFlbYmczrqK0arGeE6rNASQDklYfrUBfA"
# session_expiration = 3600
# }
# ecs_role_name = "xiaozhu"
}

0 comments on commit c71cfe3

Please sign in to comment.