Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Resource alicloud_schedulerx_namespace New Datasource alicloud_schedulerx_namespaces #5094

Merged
merged 1 commit into from Jun 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 26 additions & 0 deletions alicloud/connectivity/client.go
Expand Up @@ -4242,3 +4242,29 @@ func (client *AliyunClient) NewEdasClient() (*roa.Client, error) {
}
return conn, nil
}

func (client *AliyunClient) NewEdasschedulerxClient() (*rpc.Client, error) {
productCode := "edasschedulerx"
endpoint := ""
if v, ok := client.config.Endpoints[productCode]; !ok || v.(string) == "" {
if err := client.loadEndpoint(productCode); err != nil {
endpoint = fmt.Sprintf("edasschedulerx.%s.aliyuncs.com", client.config.RegionId)
client.config.Endpoints[productCode] = endpoint
log.Printf("[ERROR] loading %s endpoint got an error: %#v. Using the endpoint %s instead.", productCode, err, endpoint)
}
}
if v, ok := client.config.Endpoints[productCode]; ok && v.(string) != "" {
endpoint = v.(string)
}
if endpoint == "" {
return nil, fmt.Errorf("[ERROR] missing the product %s endpoint.", productCode)
}

sdkConfig := client.teaSdkConfig
sdkConfig.SetEndpoint(endpoint)
conn, err := rpc.NewClient(&sdkConfig)
if err != nil {
return nil, fmt.Errorf("unable to initialize the %s client: %#v", productCode, err)
}
return conn, nil
}
1 change: 1 addition & 0 deletions alicloud/connectivity/config.go
Expand Up @@ -165,6 +165,7 @@ type Config struct {
SmartagEndpoint string
TagEndpoint string
EdasEndpoint string
EdasschedulerxEndpoint string
}

func (c *Config) loadAndValidate() error {
Expand Down
1 change: 1 addition & 0 deletions alicloud/connectivity/regions.go
Expand Up @@ -212,3 +212,4 @@ var ACKSystemDiskEncryptionSupportRegions = []Region{Hongkong}
var DdosBasicSupportRegions = []Region{WuLanChaBu, APSouth1, HeYuan, Shenzhen, MEEast1, APSouthEast1, Huhehaote, CnNorth2Gov1, ChengDu, USEast1, Hangzhou, ShanghaiFinance, ShenZhenFinance1, GuangZhou, APSouthEast2, Beijing, EUCentral1, USWest1, APNorthEast1, Qingdao, APSouthEast3, APSouthEast5, APSouthEast6, Shanghai, Hongkong, Zhangjiakou, EUWest1}
var TagSupportRegions = []Region{Huhehaote, APSouthEast5, CnNorth2Gov1, HeYuan, APSouthEast2, Beijing, APSouthEast3, USWest1, WuLanChaBu, GuangZhou, MEEast1, ShenZhenFinance1, Shanghai, ShanghaiFinance, EUCentral1, APSouthEast1, USEast1, Hangzhou, Hongkong, Qingdao, Zhangjiakou, Shenzhen, EUWest1, APNorthEast1, APSouth1, ChengDu}
var GraphDatabaseDbInstanceSupportRegions = []Region{Hangzhou}
var SchedulerxSupportRegions = []Region{Hangzhou, Shenzhen, Beijing, Shanghai, USEast1, Zhangjiakou}
177 changes: 177 additions & 0 deletions alicloud/data_source_alicloud_schedulerx_namespaces.go
@@ -0,0 +1,177 @@
package alicloud

import (
"fmt"
"regexp"
"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"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
)

func dataSourceAlicloudSchedulerxNamespaces() *schema.Resource {
return &schema.Resource{
Read: dataSourceAlicloudSchedulerxNamespacesRead,
Schema: map[string]*schema.Schema{
"ids": {
Optional: true,
Computed: true,
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"name_regex": {
Optional: true,
ForceNew: true,
Type: schema.TypeString,
ValidateFunc: validation.ValidateRegexp,
},
"names": {
Computed: true,
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"output_file": {
Optional: true,
Type: schema.TypeString,
},
"namespaces": {
Computed: true,
Type: schema.TypeList,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Computed: true,
Type: schema.TypeString,
},
"description": {
Computed: true,
Type: schema.TypeString,
},
"namespace_id": {
Computed: true,
Type: schema.TypeString,
},
"namespace_name": {
Computed: true,
Type: schema.TypeString,
},
},
},
},
},
}
}

func dataSourceAlicloudSchedulerxNamespacesRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)

request := map[string]interface{}{
"RegionId": client.RegionId,
}

idsMap := make(map[string]string)
if v, ok := d.GetOk("ids"); ok {
for _, vv := range v.([]interface{}) {
if vv == nil {
continue
}
idsMap[vv.(string)] = vv.(string)
}
}

var namespaceNameRegex *regexp.Regexp
if v, ok := d.GetOk("name_regex"); ok {
r, err := regexp.Compile(v.(string))
if err != nil {
return WrapError(err)
}
namespaceNameRegex = r
}

conn, err := client.NewEdasschedulerxClient()
if err != nil {
return WrapError(err)
}
var objects []interface{}
var response map[string]interface{}
action := "ListNamespaces"
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(5*time.Minute, func() *resource.RetryError {
resp, err := conn.DoRequest(StringPointer(action), nil, StringPointer("GET"), StringPointer("2019-04-30"), StringPointer("AK"), request, nil, &runtime)
if err != nil {
if NeedRetry(err) {
wait()
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
response = resp
addDebug(action, response, request)
return nil
})
if err != nil {
return WrapErrorf(err, DataDefaultErrorMsg, "alicloud_schedulerx_namespaces", action, AlibabaCloudSdkGoERROR)
}
resp, err := jsonpath.Get("$.Data.Namespaces", response)
if err != nil {
return WrapErrorf(err, FailedGetAttributeMsg, action, "$.Data.Namespaces", response)
}
result, _ := resp.([]interface{})
for _, v := range result {
item := v.(map[string]interface{})
if len(idsMap) > 0 {
if _, ok := idsMap[fmt.Sprint(item["UId"])]; !ok {
continue
}
}

if namespaceNameRegex != nil && !namespaceNameRegex.MatchString(fmt.Sprint(item["Name"])) {
continue
}
objects = append(objects, item)
}

ids := make([]string, 0)
names := make([]interface{}, 0)
s := make([]map[string]interface{}, 0)
for _, v := range objects {
object := v.(map[string]interface{})
mapping := map[string]interface{}{
"id": fmt.Sprint(object["UId"]),
"description": object["Description"],
"namespace_id": object["UId"],
"namespace_name": object["Name"],
}

ids = append(ids, fmt.Sprint(object["UId"]))
names = append(names, object["Name"])

s = append(s, mapping)
}

d.SetId(dataResourceIdHash(ids))
if err := d.Set("ids", ids); err != nil {
return WrapError(err)
}
if err := d.Set("names", names); err != nil {
return WrapError(err)
}

if err := d.Set("namespaces", s); err != nil {
return WrapError(err)
}
if output, ok := d.GetOk("output_file"); ok && output.(string) != "" {
writeToFile(output.(string), s)
}
return nil
}
91 changes: 91 additions & 0 deletions alicloud/data_source_alicloud_schedulerx_namespaces_test.go
@@ -0,0 +1,91 @@
package alicloud

import (
"fmt"
"strings"
"testing"

"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
)

func TestAccAlicloudSchedulerxNamespacesDataSource(t *testing.T) {
rand := acctest.RandInt()
checkoutSupportedRegions(t, true, connectivity.SchedulerxSupportRegions)
idsConf := dataSourceTestAccConfig{
existConfig: testAccCheckAlicloudSchedulerxNamespacesDataSourceName(rand, map[string]string{
"ids": `["${alicloud_schedulerx_namespace.default.id}"]`,
}),
fakeConfig: testAccCheckAlicloudSchedulerxNamespacesDataSourceName(rand, map[string]string{
"ids": `["${alicloud_schedulerx_namespace.default.id}_fake"]`,
}),
}
nameRegexConf := dataSourceTestAccConfig{
existConfig: testAccCheckAlicloudSchedulerxNamespacesDataSourceName(rand, map[string]string{
"name_regex": `"${alicloud_schedulerx_namespace.default.namespace_name}"`,
}),
fakeConfig: testAccCheckAlicloudSchedulerxNamespacesDataSourceName(rand, map[string]string{
"name_regex": `"${alicloud_schedulerx_namespace.default.namespace_name}_fake"`,
}),
}
allConf := dataSourceTestAccConfig{
existConfig: testAccCheckAlicloudSchedulerxNamespacesDataSourceName(rand, map[string]string{
"ids": `["${alicloud_schedulerx_namespace.default.id}"]`,
"name_regex": `"${alicloud_schedulerx_namespace.default.namespace_name}"`,
}),
fakeConfig: testAccCheckAlicloudSchedulerxNamespacesDataSourceName(rand, map[string]string{
"ids": `["${alicloud_schedulerx_namespace.default.id}_fake"]`,
"name_regex": `"${alicloud_schedulerx_namespace.default.namespace_name}_fake"`,
}),
}
var existAlicloudSchedulerxNamespacesDataSourceNameMapFunc = func(rand int) map[string]string {
return map[string]string{
"ids.#": "1",
"names.#": "1",
"namespaces.#": "1",
"namespaces.0.description": fmt.Sprintf("tf-testAccNamespace-%d", rand),
"namespaces.0.namespace_name": fmt.Sprintf("tf-testAccNamespace-%d", rand),
"namespaces.0.namespace_id": CHECKSET,
"namespaces.0.id": CHECKSET,
}
}
var fakeAlicloudSchedulerxNamespacesDataSourceNameMapFunc = func(rand int) map[string]string {
return map[string]string{
"ids.#": "0",
"names.#": "0",
}
}
var alicloudSchedulerxNamespacesCheckInfo = dataSourceAttr{
resourceId: "data.alicloud_schedulerx_namespaces.default",
existMapFunc: existAlicloudSchedulerxNamespacesDataSourceNameMapFunc,
fakeMapFunc: fakeAlicloudSchedulerxNamespacesDataSourceNameMapFunc,
}

preCheck := func() {
testAccPreCheck(t)
}
alicloudSchedulerxNamespacesCheckInfo.dataSourceTestCheckWithPreCheck(t, rand, preCheck, idsConf, nameRegexConf, allConf)
}
func testAccCheckAlicloudSchedulerxNamespacesDataSourceName(rand int, attrMap map[string]string) string {
var pairs []string
for k, v := range attrMap {
pairs = append(pairs, k+" = "+v)
}

config := fmt.Sprintf(`

variable "name" {
default = "tf-testAccNamespace-%d"
}

resource "alicloud_schedulerx_namespace" "default" {
description = var.name
namespace_name = var.name
}

data "alicloud_schedulerx_namespaces" "default" {
%s
}
`, rand, strings.Join(pairs, " \n "))
return config
}
13 changes: 13 additions & 0 deletions alicloud/provider.go
Expand Up @@ -701,6 +701,7 @@ func Provider() terraform.ResourceProvider {
"alicloud_config_aggregate_deliveries": dataSourceAlicloudConfigAggregateDeliveries(),
"alicloud_edas_namespaces": dataSourceAlicloudEdasNamespaces(),
"alicloud_cdn_blocked_regions": dataSourceAlicloudCdnBlockedRegions(),
"alicloud_schedulerx_namespaces": dataSourceAlicloudSchedulerxNamespaces(),
},
ResourcesMap: map[string]*schema.Resource{
"alicloud_instance": resourceAliyunInstance(),
Expand Down Expand Up @@ -1298,6 +1299,7 @@ func Provider() terraform.ResourceProvider {
"alicloud_cms_sls_group": resourceAlicloudCmsSlsGroup(),
"alicloud_config_aggregate_delivery": resourceAlicloudConfigAggregateDelivery(),
"alicloud_edas_namespace": resourceAlicloudEdasNamespace(),
"alicloud_schedulerx_namespace": resourceAlicloudSchedulerxNamespace(),
},

ConfigureFunc: providerConfigure,
Expand Down Expand Up @@ -1517,6 +1519,7 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
config.SmartagEndpoint = strings.TrimSpace(endpoints["smartag"].(string))
config.TagEndpoint = strings.TrimSpace(endpoints["tag"].(string))
config.EdasEndpoint = strings.TrimSpace(endpoints["edas"].(string))
config.EdasschedulerxEndpoint = strings.TrimSpace(endpoints["edasschedulerx"].(string))
if endpoint, ok := endpoints["alidns"]; ok {
config.AlidnsEndpoint = strings.TrimSpace(endpoint.(string))
} else {
Expand Down Expand Up @@ -1835,6 +1838,8 @@ func init() {
"tag_endpoint": "Use this to override the default endpoint URL constructed from the `region`. It's typically used to connect to custom tag endpoints.",

"edas_endpoint": "Use this to override the default endpoint URL constructed from the `region`. It's typically used to connect to custom edas endpoints.",

"edasschedulerx_endpoint": "Use this to override the default endpoint URL constructed from the `region`. It's typically used to connect to custom edasschedulerx endpoints.",
}
}

Expand Down Expand Up @@ -1886,6 +1891,13 @@ func endpointsSchema() *schema.Schema {
Description: descriptions["edas_endpoint"],
},

"edasschedulerx": {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: descriptions["edasschedulerx_endpoint"],
},

"tag": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -2726,6 +2738,7 @@ func endpointsToHash(v interface{}) int {
buf.WriteString(fmt.Sprintf("%s-", m["smartag"].(string)))
buf.WriteString(fmt.Sprintf("%s-", m["tag"].(string)))
buf.WriteString(fmt.Sprintf("%s-", m["edas"].(string)))
buf.WriteString(fmt.Sprintf("%s-", m["edasschedulerx"].(string)))
return hashcode.String(buf.String())
}

Expand Down