diff --git a/.changelog/2865.txt b/.changelog/2865.txt new file mode 100644 index 0000000000..5d1e5b2455 --- /dev/null +++ b/.changelog/2865.txt @@ -0,0 +1,15 @@ +```release-note:enhancement +tencentcloud_teo_function +``` + +```release-note:enhancement +tencentcloud_teo_function_rule +``` + +```release-note:enhancement +tencentcloud_teo_function_rule_priority +``` + +```release-note:enhancement +tencentcloud_teo_function_runtime_environment +``` diff --git a/tencentcloud/provider.go b/tencentcloud/provider.go index 97d942afac..f0e6c29652 100644 --- a/tencentcloud/provider.go +++ b/tencentcloud/provider.go @@ -1703,6 +1703,10 @@ func Provider() *schema.Provider { "tencentcloud_teo_application_proxy_rule": teo.ResourceTencentCloudTeoApplicationProxyRule(), "tencentcloud_teo_realtime_log_delivery": teo.ResourceTencentCloudTeoRealtimeLogDelivery(), "tencentcloud_teo_security_ip_group": teo.ResourceTencentCloudTeoSecurityIpGroup(), + "tencentcloud_teo_function": teo.ResourceTencentCloudTeoFunction(), + "tencentcloud_teo_function_rule": teo.ResourceTencentCloudTeoFunctionRule(), + "tencentcloud_teo_function_rule_priority": teo.ResourceTencentCloudTeoFunctionRulePriority(), + "tencentcloud_teo_function_runtime_environment": teo.ResourceTencentCloudTeoFunctionRuntimeEnvironment(), "tencentcloud_tcm_mesh": tcm.ResourceTencentCloudTcmMesh(), "tencentcloud_tcm_cluster_attachment": tcm.ResourceTencentCloudTcmClusterAttachment(), "tencentcloud_tcm_prometheus_attachment": tcm.ResourceTencentCloudTcmPrometheusAttachment(), diff --git a/tencentcloud/provider.md b/tencentcloud/provider.md index 2714a3d8c6..030ab051cb 100644 --- a/tencentcloud/provider.md +++ b/tencentcloud/provider.md @@ -1433,6 +1433,10 @@ TencentCloud EdgeOne(TEO) tencentcloud_teo_acceleration_domain tencentcloud_teo_l4_proxy tencentcloud_teo_realtime_log_delivery + tencentcloud_teo_function + tencentcloud_teo_function_rule + tencentcloud_teo_function_rule_priority + tencentcloud_teo_function_runtime_environment TencentCloud ServiceMesh(TCM) Data Source diff --git a/tencentcloud/services/teo/resource_tc_teo_function.go b/tencentcloud/services/teo/resource_tc_teo_function.go new file mode 100644 index 0000000000..26c2a5028f --- /dev/null +++ b/tencentcloud/services/teo/resource_tc_teo_function.go @@ -0,0 +1,362 @@ +// Code generated by iacg; DO NOT EDIT. +package teo + +import ( + "bytes" + "context" + "fmt" + "io" + "log" + "strings" + "text/template" + "time" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + teov20220901 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/teo/v20220901" + + tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" +) + +func ResourceTencentCloudTeoFunction() *schema.Resource { + return &schema.Resource{ + Create: resourceTencentCloudTeoFunctionCreate, + Read: resourceTencentCloudTeoFunctionRead, + Update: resourceTencentCloudTeoFunctionUpdate, + Delete: resourceTencentCloudTeoFunctionDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + Schema: map[string]*schema.Schema{ + "zone_id": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + Description: "ID of the site.", + }, + + "function_id": { + Type: schema.TypeString, + Computed: true, + Description: "ID of the Function.", + }, + + "name": { + Type: schema.TypeString, + Required: true, + Description: "Function name. It can only contain lowercase letters, numbers, hyphens, must start and end with a letter or number, and can have a maximum length of 30 characters.", + }, + + "remark": { + Type: schema.TypeString, + Optional: true, + Description: "Function description, maximum support of 60 characters.", + }, + + "content": { + Type: schema.TypeString, + Required: true, + Description: "Function content, currently only supports JavaScript code, with a maximum size of 5MB.", + }, + + "domain": { + Type: schema.TypeString, + Computed: true, + Description: "The default domain name for the function.", + }, + + "create_time": { + Type: schema.TypeString, + Computed: true, + Description: "Creation time. The time is in Coordinated Universal Time (UTC) and follows the date and time format specified by the ISO 8601 standard.", + }, + + "update_time": { + Type: schema.TypeString, + Computed: true, + Description: "Modification time. The time is in Coordinated Universal Time (UTC) and follows the date and time format specified by the ISO 8601 standard.", + }, + }, + } +} + +func resourceTencentCloudTeoFunctionCreate(d *schema.ResourceData, meta interface{}) error { + defer tccommon.LogElapsed("resource.tencentcloud_teo_function.create")() + defer tccommon.InconsistentCheck(d, meta)() + + logId := tccommon.GetLogId(tccommon.ContextNil) + + ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) + + var ( + zoneId string + functionId string + ) + var ( + request = teov20220901.NewCreateFunctionRequest() + response = teov20220901.NewCreateFunctionResponse() + ) + + if v, ok := d.GetOk("zone_id"); ok { + zoneId = v.(string) + } + + request.ZoneId = helper.String(zoneId) + + if v, ok := d.GetOk("name"); ok { + request.Name = helper.String(v.(string)) + } + + if v, ok := d.GetOk("remark"); ok { + request.Remark = helper.String(v.(string)) + } + + if v, ok := d.GetOk("content"); ok { + request.Content = helper.String(v.(string)) + } + + err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseTeoV20220901Client().CreateFunctionWithContext(ctx, request) + if e != nil { + return tccommon.RetryError(e) + } else { + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) + } + response = result + return nil + }) + if err != nil { + log.Printf("[CRITAL]%s create teo function failed, reason:%+v", logId, err) + return err + } + + functionId = *response.Response.FunctionId + + if _, err := (&resource.StateChangeConf{ + Delay: 10 * time.Second, + MinTimeout: 3 * time.Second, + Pending: []string{"false"}, + Refresh: resourceTeoFunctionCreateStateRefreshFunc_0_0(ctx, zoneId, functionId), + Target: []string{"true"}, + Timeout: 600 * time.Second, + }).WaitForStateContext(ctx); err != nil { + return err + } + d.SetId(strings.Join([]string{zoneId, functionId}, tccommon.FILED_SP)) + + return resourceTencentCloudTeoFunctionRead(d, meta) +} + +func resourceTencentCloudTeoFunctionRead(d *schema.ResourceData, meta interface{}) error { + defer tccommon.LogElapsed("resource.tencentcloud_teo_function.read")() + defer tccommon.InconsistentCheck(d, meta)() + + logId := tccommon.GetLogId(tccommon.ContextNil) + + ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) + + service := TeoService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} + + idSplit := strings.Split(d.Id(), tccommon.FILED_SP) + if len(idSplit) != 2 { + return fmt.Errorf("id is broken,%s", d.Id()) + } + zoneId := idSplit[0] + functionId := idSplit[1] + + _ = d.Set("zone_id", zoneId) + + respData, err := service.DescribeTeoFunctionById(ctx, zoneId, functionId) + if err != nil { + return err + } + + if respData == nil { + d.SetId("") + log.Printf("[WARN]%s resource `teo_function` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) + return nil + } + if respData.FunctionId != nil { + _ = d.Set("function_id", respData.FunctionId) + functionId = *respData.FunctionId + } + + if respData.Name != nil { + _ = d.Set("name", respData.Name) + } + + if respData.Remark != nil { + _ = d.Set("remark", respData.Remark) + } + + if respData.Content != nil { + _ = d.Set("content", respData.Content) + } + + if respData.Domain != nil { + _ = d.Set("domain", respData.Domain) + } + + if respData.CreateTime != nil { + _ = d.Set("create_time", respData.CreateTime) + } + + if respData.UpdateTime != nil { + _ = d.Set("update_time", respData.UpdateTime) + } + + return nil +} + +func resourceTencentCloudTeoFunctionUpdate(d *schema.ResourceData, meta interface{}) error { + defer tccommon.LogElapsed("resource.tencentcloud_teo_function.update")() + defer tccommon.InconsistentCheck(d, meta)() + + logId := tccommon.GetLogId(tccommon.ContextNil) + + ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) + + immutableArgs := []string{"name"} + for _, v := range immutableArgs { + if d.HasChange(v) { + return fmt.Errorf("argument `%s` cannot be changed", v) + } + } + idSplit := strings.Split(d.Id(), tccommon.FILED_SP) + if len(idSplit) != 2 { + return fmt.Errorf("id is broken,%s", d.Id()) + } + zoneId := idSplit[0] + functionId := idSplit[1] + + needChange := false + mutableArgs := []string{"remark", "content"} + for _, v := range mutableArgs { + if d.HasChange(v) { + needChange = true + break + } + } + + if needChange { + request := teov20220901.NewModifyFunctionRequest() + + request.ZoneId = helper.String(zoneId) + + request.FunctionId = helper.String(functionId) + + if v, ok := d.GetOk("remark"); ok { + request.Remark = helper.String(v.(string)) + } + + if v, ok := d.GetOk("content"); ok { + request.Content = helper.String(v.(string)) + } + + err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseTeoV20220901Client().ModifyFunctionWithContext(ctx, request) + if e != nil { + return tccommon.RetryError(e) + } else { + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) + } + return nil + }) + if err != nil { + log.Printf("[CRITAL]%s update teo function failed, reason:%+v", logId, err) + return err + } + } + + return resourceTencentCloudTeoFunctionRead(d, meta) +} + +func resourceTencentCloudTeoFunctionDelete(d *schema.ResourceData, meta interface{}) error { + defer tccommon.LogElapsed("resource.tencentcloud_teo_function.delete")() + defer tccommon.InconsistentCheck(d, meta)() + + logId := tccommon.GetLogId(tccommon.ContextNil) + ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) + + idSplit := strings.Split(d.Id(), tccommon.FILED_SP) + if len(idSplit) != 2 { + return fmt.Errorf("id is broken,%s", d.Id()) + } + zoneId := idSplit[0] + functionId := idSplit[1] + + var ( + request = teov20220901.NewDeleteFunctionRequest() + response = teov20220901.NewDeleteFunctionResponse() + ) + + request.ZoneId = helper.String(zoneId) + + request.FunctionId = helper.String(functionId) + + err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseTeoV20220901Client().DeleteFunctionWithContext(ctx, request) + if e != nil { + return tccommon.RetryError(e) + } else { + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) + } + response = result + return nil + }) + if err != nil { + log.Printf("[CRITAL]%s delete teo function failed, reason:%+v", logId, err) + return err + } + + _ = response + return nil +} + +func resourceTeoFunctionCreateStateRefreshFunc_0_0(ctx context.Context, zoneId string, functionId string) resource.StateRefreshFunc { + var req *teov20220901.DescribeFunctionsRequest + t := template.New("gotpl") + var tplObj *template.Template + return func() (interface{}, string, error) { + meta := tccommon.ProviderMetaFromContext(ctx) + if meta == nil { + return nil, "", fmt.Errorf("resource data can not be nil") + } + if req == nil { + d := tccommon.ResourceDataFromContext(ctx) + if d == nil { + return nil, "", fmt.Errorf("resource data can not be nil") + } + _ = d + req = teov20220901.NewDescribeFunctionsRequest() + req.ZoneId = helper.String(zoneId) + + req.FunctionIds = []*string{helper.String(functionId)} + + } + resp, err := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseTeoV20220901Client().DescribeFunctionsWithContext(ctx, req) + if err != nil { + return nil, "", err + } + if resp == nil || resp.Response == nil { + return nil, "", nil + } + if tplObj == nil { + tplObj, err = t.Parse("{{ if .Functions }}{{ $firstFunction := index .Functions 0 }}{{ if $firstFunction.Domain }}{{ true }}{{ else }}{{ false }}{{ end }}{{ end }}") + if err != nil { + return resp.Response, "", fmt.Errorf("parse state go-template error: %w", err) + } + } + stream := new(bytes.Buffer) + if err := tplObj.Execute(stream, resp.Response); err != nil { + return resp.Response, "", err + } + stateBytes, err := io.ReadAll(stream) + if err != nil { + return resp.Response, "", err + } + state := string(stateBytes) + return resp.Response, state, nil + } +} diff --git a/tencentcloud/services/teo/resource_tc_teo_function.md b/tencentcloud/services/teo/resource_tc_teo_function.md new file mode 100644 index 0000000000..a58d58240c --- /dev/null +++ b/tencentcloud/services/teo/resource_tc_teo_function.md @@ -0,0 +1,25 @@ +Provides a resource to create a teo teo_function + +Example Usage + +```hcl +resource "tencentcloud_teo_function" "teo_function" { + content = <<-EOT + addEventListener('fetch', e => { + const response = new Response('Hello World!!'); + e.respondWith(response); + }); + EOT + name = "aaa-zone-2qtuhspy7cr6-1310708577" + remark = "test" + zone_id = "zone-2qtuhspy7cr6" +} +``` + +Import + +teo teo_function can be imported using the id, e.g. + +``` +terraform import tencentcloud_teo_function.teo_function zone_id#function_id +``` diff --git a/tencentcloud/services/teo/resource_tc_teo_function_extension.go b/tencentcloud/services/teo/resource_tc_teo_function_extension.go new file mode 100644 index 0000000000..95661f5fba --- /dev/null +++ b/tencentcloud/services/teo/resource_tc_teo_function_extension.go @@ -0,0 +1 @@ +package teo diff --git a/tencentcloud/services/teo/resource_tc_teo_function_rule.go b/tencentcloud/services/teo/resource_tc_teo_function_rule.go new file mode 100644 index 0000000000..5aa829a613 --- /dev/null +++ b/tencentcloud/services/teo/resource_tc_teo_function_rule.go @@ -0,0 +1,434 @@ +// Code generated by iacg; DO NOT EDIT. +package teo + +import ( + "context" + "fmt" + "log" + "strings" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + teov20220901 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/teo/v20220901" + + tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" +) + +func ResourceTencentCloudTeoFunctionRule() *schema.Resource { + return &schema.Resource{ + Create: resourceTencentCloudTeoFunctionRuleCreate, + Read: resourceTencentCloudTeoFunctionRuleRead, + Update: resourceTencentCloudTeoFunctionRuleUpdate, + Delete: resourceTencentCloudTeoFunctionRuleDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + Schema: map[string]*schema.Schema{ + "zone_id": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + Description: "ID of the site.", + }, + + "rule_id": { + Type: schema.TypeString, + Computed: true, + Description: "ID of the Function Rule.", + }, + + "function_id": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + Description: "ID of the Function.", + }, + + "remark": { + Type: schema.TypeString, + Optional: true, + Description: "Rule description, maximum support of 60 characters.", + }, + + "function_name": { + Type: schema.TypeString, + Computed: true, + Description: "The name of the function.", + }, + + "priority": { + Type: schema.TypeInt, + Computed: true, + Description: "The priority of the function trigger rule. A higher numerical value indicates a higher priority.", + }, + + "function_rule_conditions": { + Type: schema.TypeList, + Required: true, + Description: "The list of rule conditions, where the conditions are connected by an \"OR\" relationship.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "rule_conditions": { + Type: schema.TypeList, + Required: true, + Description: "For edge function trigger rule conditions, if all items in the list are satisfied, then the condition is considered fulfilled.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "operator": { + Type: schema.TypeString, + Required: true, + Description: "Operator. Valid values:\n - `equals`: Equals.\n - `notEquals`: Does not equal.\n - `exist`: Exists.\n - `notexist`: Does not exist.", + }, + "target": { + Type: schema.TypeString, + Required: true, + Description: "The match type. Values:\n - `filename`: File name.\n - `extension`: File extension.\n - `host`: Host.\n - `full_url`: Full URL, which indicates the complete URL path under the current site and must contain the HTTP protocol, host, and path.\n - `url`: Partial URL under the current site.\n - `client_country`: Country/Region of the client.\n - `query_string`: Query string in the request URL.\n - `request_header`: HTTP request header.", + }, + "values": { + Type: schema.TypeSet, + Optional: true, + Description: "The parameter value of the match type. It can be an empty string only when `Target=query string/request header` and `Operator=exist/notexist`.\n - When `Target=extension`, enter the file extension, such as \"jpg\" and \"txt\".\n - When `Target=filename`, enter the file name, such as \"foo\" in \"foo.jpg\".\n - When `Target=all`, it indicates any site request.\n - When `Target=host`, enter the host under the current site, such as \"www.maxx55.com\".\n - When `Target=url`, enter the partial URL path under the current site, such as \"/example\".\n - When `Target=full_url`, enter the complete URL under the current site. It must contain the HTTP protocol, host, and path, such as \"https://www.maxx55.cn/example\".\n - When `Target=client_country`, enter the ISO-3166 country/region code.\n - When `Target=query_string`, enter the value of the query string, such as \"cn\" and \"1\" in \"lang=cn&version=1\".\n - When `Target=request_header`, enter the HTTP request header value, such as \"zh-CN,zh;q=0.9\" in the \"Accept-Language:zh-CN,zh;q=0.9\" header.", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "ignore_case": { + Type: schema.TypeBool, + Optional: true, + Description: "Whether the parameter value is case insensitive. Default value: false.", + }, + "name": { + Type: schema.TypeString, + Optional: true, + Description: "The parameter name of the match type. This field is required only when `Target=query_string/request_header`.\n - `query_string`: Name of the query string, such as \"lang\" and \"version\" in \"lang=cn&version=1\".\n - `request_header`: Name of the HTTP request header, such as \"Accept-Language\" in the \"Accept-Language:zh-CN,zh;q=0.9\" header.", + }, + }, + }, + }, + }, + }, + }, + }, + } +} + +func resourceTencentCloudTeoFunctionRuleCreate(d *schema.ResourceData, meta interface{}) error { + defer tccommon.LogElapsed("resource.tencentcloud_teo_function_rule.create")() + defer tccommon.InconsistentCheck(d, meta)() + + logId := tccommon.GetLogId(tccommon.ContextNil) + + ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) + + var ( + zoneId string + functionId string + ruleId string + ) + var ( + request = teov20220901.NewCreateFunctionRuleRequest() + response = teov20220901.NewCreateFunctionRuleResponse() + ) + + if v, ok := d.GetOk("zone_id"); ok { + zoneId = v.(string) + } + if v, ok := d.GetOk("function_id"); ok { + functionId = v.(string) + } + + request.ZoneId = helper.String(zoneId) + + if v, ok := d.GetOk("function_rule_conditions"); ok { + for _, item := range v.([]interface{}) { + functionRuleConditionsMap := item.(map[string]interface{}) + functionRuleCondition := teov20220901.FunctionRuleCondition{} + if v, ok := functionRuleConditionsMap["rule_conditions"]; ok { + for _, item := range v.([]interface{}) { + ruleConditionsMap := item.(map[string]interface{}) + ruleCondition := teov20220901.RuleCondition{} + if v, ok := ruleConditionsMap["operator"]; ok { + ruleCondition.Operator = helper.String(v.(string)) + } + if v, ok := ruleConditionsMap["target"]; ok { + ruleCondition.Target = helper.String(v.(string)) + } + if v, ok := ruleConditionsMap["values"]; ok { + valuesSet := v.(*schema.Set).List() + for i := range valuesSet { + values := valuesSet[i].(string) + ruleCondition.Values = append(ruleCondition.Values, helper.String(values)) + } + } + if v, ok := ruleConditionsMap["ignore_case"]; ok { + ruleCondition.IgnoreCase = helper.Bool(v.(bool)) + } + if v, ok := ruleConditionsMap["name"]; ok { + ruleCondition.Name = helper.String(v.(string)) + } + functionRuleCondition.RuleConditions = append(functionRuleCondition.RuleConditions, &ruleCondition) + } + } + request.FunctionRuleConditions = append(request.FunctionRuleConditions, &functionRuleCondition) + } + } + + request.FunctionId = helper.String(functionId) + + if v, ok := d.GetOk("remark"); ok { + request.Remark = helper.String(v.(string)) + } + + err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseTeoV20220901Client().CreateFunctionRuleWithContext(ctx, request) + if e != nil { + return tccommon.RetryError(e) + } else { + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) + } + response = result + return nil + }) + if err != nil { + log.Printf("[CRITAL]%s create teo function rule failed, reason:%+v", logId, err) + return err + } + + ruleId = *response.Response.RuleId + + d.SetId(strings.Join([]string{zoneId, functionId, ruleId}, tccommon.FILED_SP)) + + return resourceTencentCloudTeoFunctionRuleRead(d, meta) +} + +func resourceTencentCloudTeoFunctionRuleRead(d *schema.ResourceData, meta interface{}) error { + defer tccommon.LogElapsed("resource.tencentcloud_teo_function_rule.read")() + defer tccommon.InconsistentCheck(d, meta)() + + logId := tccommon.GetLogId(tccommon.ContextNil) + + ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) + + service := TeoService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} + + idSplit := strings.Split(d.Id(), tccommon.FILED_SP) + if len(idSplit) != 3 { + return fmt.Errorf("id is broken,%s", d.Id()) + } + zoneId := idSplit[0] + functionId := idSplit[1] + ruleId := idSplit[2] + + _ = d.Set("zone_id", zoneId) + + _ = d.Set("function_id", functionId) + + respData, err := service.DescribeTeoFunctionRuleById(ctx, zoneId, functionId, ruleId) + if err != nil { + return err + } + + if respData == nil { + d.SetId("") + log.Printf("[WARN]%s resource `teo_function_rule` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) + return nil + } + if respData.RuleId != nil { + _ = d.Set("rule_id", respData.RuleId) + ruleId = *respData.RuleId + } + + if respData.FunctionId != nil { + _ = d.Set("function_id", respData.FunctionId) + } + + if respData.FunctionName != nil { + _ = d.Set("function_name", respData.FunctionName) + } + + if respData.Remark != nil { + _ = d.Set("remark", respData.Remark) + } + + if respData.Priority != nil { + _ = d.Set("priority", respData.Priority) + } + + functionRuleConditionsList := make([]map[string]interface{}, 0, len(respData.FunctionRuleConditions)) + if respData.FunctionRuleConditions != nil { + for _, functionRuleConditions := range respData.FunctionRuleConditions { + functionRuleConditionsMap := map[string]interface{}{} + + ruleConditionsList := make([]map[string]interface{}, 0, len(functionRuleConditions.RuleConditions)) + if functionRuleConditions.RuleConditions != nil { + for _, ruleConditions := range functionRuleConditions.RuleConditions { + ruleConditionsMap := map[string]interface{}{} + + if ruleConditions.Operator != nil { + ruleConditionsMap["operator"] = ruleConditions.Operator + } + + if ruleConditions.Target != nil { + ruleConditionsMap["target"] = ruleConditions.Target + } + + if ruleConditions.Values != nil { + ruleConditionsMap["values"] = ruleConditions.Values + } + + if ruleConditions.IgnoreCase != nil { + ruleConditionsMap["ignore_case"] = ruleConditions.IgnoreCase + } + + if ruleConditions.Name != nil { + ruleConditionsMap["name"] = ruleConditions.Name + } + + ruleConditionsList = append(ruleConditionsList, ruleConditionsMap) + } + + functionRuleConditionsMap["rule_conditions"] = ruleConditionsList + } + functionRuleConditionsList = append(functionRuleConditionsList, functionRuleConditionsMap) + } + + _ = d.Set("function_rule_conditions", functionRuleConditionsList) + } + + return nil +} + +func resourceTencentCloudTeoFunctionRuleUpdate(d *schema.ResourceData, meta interface{}) error { + defer tccommon.LogElapsed("resource.tencentcloud_teo_function_rule.update")() + defer tccommon.InconsistentCheck(d, meta)() + + logId := tccommon.GetLogId(tccommon.ContextNil) + + ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) + + idSplit := strings.Split(d.Id(), tccommon.FILED_SP) + if len(idSplit) != 3 { + return fmt.Errorf("id is broken,%s", d.Id()) + } + zoneId := idSplit[0] + functionId := idSplit[1] + ruleId := idSplit[2] + + needChange := false + mutableArgs := []string{"function_rule_conditions", "remark"} + for _, v := range mutableArgs { + if d.HasChange(v) { + needChange = true + break + } + } + + if needChange { + request := teov20220901.NewModifyFunctionRuleRequest() + + request.ZoneId = helper.String(zoneId) + + request.RuleId = helper.String(ruleId) + + if v, ok := d.GetOk("function_rule_conditions"); ok { + for _, item := range v.([]interface{}) { + functionRuleConditionsMap := item.(map[string]interface{}) + functionRuleCondition := teov20220901.FunctionRuleCondition{} + if v, ok := functionRuleConditionsMap["rule_conditions"]; ok { + for _, item := range v.([]interface{}) { + ruleConditionsMap := item.(map[string]interface{}) + ruleCondition := teov20220901.RuleCondition{} + if v, ok := ruleConditionsMap["operator"]; ok { + ruleCondition.Operator = helper.String(v.(string)) + } + if v, ok := ruleConditionsMap["target"]; ok { + ruleCondition.Target = helper.String(v.(string)) + } + if v, ok := ruleConditionsMap["values"]; ok { + valuesSet := v.(*schema.Set).List() + for i := range valuesSet { + values := valuesSet[i].(string) + ruleCondition.Values = append(ruleCondition.Values, helper.String(values)) + } + } + if v, ok := ruleConditionsMap["ignore_case"]; ok { + ruleCondition.IgnoreCase = helper.Bool(v.(bool)) + } + if v, ok := ruleConditionsMap["name"]; ok { + ruleCondition.Name = helper.String(v.(string)) + } + functionRuleCondition.RuleConditions = append(functionRuleCondition.RuleConditions, &ruleCondition) + } + } + request.FunctionRuleConditions = append(request.FunctionRuleConditions, &functionRuleCondition) + } + } + + request.FunctionId = helper.String(functionId) + + if v, ok := d.GetOk("remark"); ok { + request.Remark = helper.String(v.(string)) + } + + err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseTeoV20220901Client().ModifyFunctionRuleWithContext(ctx, request) + if e != nil { + return tccommon.RetryError(e) + } else { + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) + } + return nil + }) + if err != nil { + log.Printf("[CRITAL]%s update teo function rule failed, reason:%+v", logId, err) + return err + } + } + + return resourceTencentCloudTeoFunctionRuleRead(d, meta) +} + +func resourceTencentCloudTeoFunctionRuleDelete(d *schema.ResourceData, meta interface{}) error { + defer tccommon.LogElapsed("resource.tencentcloud_teo_function_rule.delete")() + defer tccommon.InconsistentCheck(d, meta)() + + logId := tccommon.GetLogId(tccommon.ContextNil) + ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) + + idSplit := strings.Split(d.Id(), tccommon.FILED_SP) + if len(idSplit) != 3 { + return fmt.Errorf("id is broken,%s", d.Id()) + } + zoneId := idSplit[0] + functionId := idSplit[1] + ruleId := idSplit[2] + + var ( + request = teov20220901.NewDeleteFunctionRulesRequest() + response = teov20220901.NewDeleteFunctionRulesResponse() + ) + + request.ZoneId = helper.String(zoneId) + + request.RuleIds = []*string{helper.String(ruleId)} + + err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseTeoV20220901Client().DeleteFunctionRulesWithContext(ctx, request) + if e != nil { + return tccommon.RetryError(e) + } else { + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) + } + response = result + return nil + }) + if err != nil { + log.Printf("[CRITAL]%s delete teo function rule failed, reason:%+v", logId, err) + return err + } + + _ = response + _ = functionId + return nil +} diff --git a/tencentcloud/services/teo/resource_tc_teo_function_rule.md b/tencentcloud/services/teo/resource_tc_teo_function_rule.md new file mode 100644 index 0000000000..b51a7a3e10 --- /dev/null +++ b/tencentcloud/services/teo/resource_tc_teo_function_rule.md @@ -0,0 +1,60 @@ +Provides a resource to create a teo teo_function_rule + +Example Usage + +```hcl +resource "tencentcloud_teo_function_rule" "teo_function_rule" { + function_id = "ef-txx7fnua" + remark = "aaa" + zone_id = "zone-2qtuhspy7cr6" + + function_rule_conditions { + rule_conditions { + ignore_case = false + name = null + operator = "equal" + target = "host" + values = [ + "aaa.makn.cn", + ] + } + rule_conditions { + ignore_case = false + name = null + operator = "equal" + target = "extension" + values = [ + ".txt", + ] + } + } + function_rule_conditions { + rule_conditions { + ignore_case = false + name = null + operator = "notequal" + target = "host" + values = [ + "aaa.makn.cn", + ] + } + rule_conditions { + ignore_case = false + name = null + operator = "equal" + target = "extension" + values = [ + ".png", + ] + } + } +} +``` + +Import + +teo teo_function_rule can be imported using the id, e.g. + +``` +terraform import tencentcloud_teo_function_rule.teo_function_rule zone_id#function_id#rule_id +``` diff --git a/tencentcloud/services/teo/resource_tc_teo_function_rule_extension.go b/tencentcloud/services/teo/resource_tc_teo_function_rule_extension.go new file mode 100644 index 0000000000..95661f5fba --- /dev/null +++ b/tencentcloud/services/teo/resource_tc_teo_function_rule_extension.go @@ -0,0 +1 @@ +package teo diff --git a/tencentcloud/services/teo/resource_tc_teo_function_rule_priority.go b/tencentcloud/services/teo/resource_tc_teo_function_rule_priority.go new file mode 100644 index 0000000000..f10ad369b7 --- /dev/null +++ b/tencentcloud/services/teo/resource_tc_teo_function_rule_priority.go @@ -0,0 +1,173 @@ +// Code generated by iacg; DO NOT EDIT. +package teo + +import ( + "context" + "fmt" + "log" + "strings" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + teov20220901 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/teo/v20220901" + + tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" +) + +func ResourceTencentCloudTeoFunctionRulePriority() *schema.Resource { + return &schema.Resource{ + Create: resourceTencentCloudTeoFunctionRulePriorityCreate, + Read: resourceTencentCloudTeoFunctionRulePriorityRead, + Update: resourceTencentCloudTeoFunctionRulePriorityUpdate, + Delete: resourceTencentCloudTeoFunctionRulePriorityDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + Schema: map[string]*schema.Schema{ + "zone_id": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + Description: "ID of the site.", + }, + + "function_id": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + Description: "ID of the Function.", + }, + + "rule_ids": { + Type: schema.TypeList, + Required: true, + Description: "he list of rule IDs. It is required to include all rule IDs after adjusting their priorities. The execution order of multiple rules follows a top-down sequence. If not specified, the original priority order will be maintained.", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + }, + } +} + +func resourceTencentCloudTeoFunctionRulePriorityCreate(d *schema.ResourceData, meta interface{}) error { + defer tccommon.LogElapsed("resource.tencentcloud_teo_function_rule_priority.create")() + defer tccommon.InconsistentCheck(d, meta)() + + var ( + zoneId string + functionId string + ) + if v, ok := d.GetOk("zone_id"); ok { + zoneId = v.(string) + } + if v, ok := d.GetOk("function_id"); ok { + functionId = v.(string) + } + + d.SetId(strings.Join([]string{zoneId, functionId}, tccommon.FILED_SP)) + + return resourceTencentCloudTeoFunctionRulePriorityUpdate(d, meta) +} + +func resourceTencentCloudTeoFunctionRulePriorityRead(d *schema.ResourceData, meta interface{}) error { + defer tccommon.LogElapsed("resource.tencentcloud_teo_function_rule_priority.read")() + defer tccommon.InconsistentCheck(d, meta)() + + logId := tccommon.GetLogId(tccommon.ContextNil) + + ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) + + service := TeoService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} + + idSplit := strings.Split(d.Id(), tccommon.FILED_SP) + if len(idSplit) != 2 { + return fmt.Errorf("id is broken,%s", d.Id()) + } + zoneId := idSplit[0] + functionId := idSplit[1] + + _ = d.Set("zone_id", zoneId) + + _ = d.Set("function_id", functionId) + + respData, err := service.DescribeTeoFunctionRulePriorityById(ctx, zoneId, functionId) + if err != nil { + return err + } + + if respData == nil { + d.SetId("") + log.Printf("[WARN]%s resource `teo_function_rule_priority` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) + return nil + } + if err := resourceTencentCloudTeoFunctionRulePriorityReadPreHandleResponse0(ctx, respData); err != nil { + return err + } + + return nil +} + +func resourceTencentCloudTeoFunctionRulePriorityUpdate(d *schema.ResourceData, meta interface{}) error { + defer tccommon.LogElapsed("resource.tencentcloud_teo_function_rule_priority.update")() + defer tccommon.InconsistentCheck(d, meta)() + + logId := tccommon.GetLogId(tccommon.ContextNil) + + ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) + + idSplit := strings.Split(d.Id(), tccommon.FILED_SP) + if len(idSplit) != 2 { + return fmt.Errorf("id is broken,%s", d.Id()) + } + zoneId := idSplit[0] + functionId := idSplit[1] + + needChange := false + mutableArgs := []string{"rule_ids"} + for _, v := range mutableArgs { + if d.HasChange(v) { + needChange = true + break + } + } + + if needChange { + request := teov20220901.NewModifyFunctionRulePriorityRequest() + + request.ZoneId = helper.String(zoneId) + + if v, ok := d.GetOk("rule_ids"); ok { + ruleIdsSet := v.([]interface{}) + for i := range ruleIdsSet { + ruleIds := ruleIdsSet[i].(string) + request.RuleIds = append(request.RuleIds, helper.String(ruleIds)) + } + } + + err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseTeoV20220901Client().ModifyFunctionRulePriorityWithContext(ctx, request) + if e != nil { + return tccommon.RetryError(e) + } else { + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) + } + return nil + }) + if err != nil { + log.Printf("[CRITAL]%s update teo function rule priority failed, reason:%+v", logId, err) + return err + } + } + + _ = functionId + return resourceTencentCloudTeoFunctionRulePriorityRead(d, meta) +} + +func resourceTencentCloudTeoFunctionRulePriorityDelete(d *schema.ResourceData, meta interface{}) error { + defer tccommon.LogElapsed("resource.tencentcloud_teo_function_rule_priority.delete")() + defer tccommon.InconsistentCheck(d, meta)() + + return nil +} diff --git a/tencentcloud/services/teo/resource_tc_teo_function_rule_priority.md b/tencentcloud/services/teo/resource_tc_teo_function_rule_priority.md new file mode 100644 index 0000000000..750e27eae5 --- /dev/null +++ b/tencentcloud/services/teo/resource_tc_teo_function_rule_priority.md @@ -0,0 +1,22 @@ +Provides a resource to create a teo teo_function_rule_priority + +Example Usage + +```hcl +resource "tencentcloud_teo_function_rule_priority" "teo_function_rule_priority" { + function_id = "ef-txx7fnua" + rule_ids = [ + "rule-equpbht3", + "rule-ax28n3g6", + ] + zone_id = "zone-2qtuhspy7cr6" +} +``` + +Import + +teo teo_function_rule_priority can be imported using the id, e.g. + +``` +terraform import tencentcloud_teo_function_rule_priority.teo_function_rule_priority zone_id#function_id +``` diff --git a/tencentcloud/services/teo/resource_tc_teo_function_rule_priority_extension.go b/tencentcloud/services/teo/resource_tc_teo_function_rule_priority_extension.go new file mode 100644 index 0000000000..6a31767f80 --- /dev/null +++ b/tencentcloud/services/teo/resource_tc_teo_function_rule_priority_extension.go @@ -0,0 +1,25 @@ +package teo + +import ( + "context" + + teov20220901 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/teo/v20220901" + tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" +) + +func resourceTencentCloudTeoFunctionRulePriorityReadPreHandleResponse0(ctx context.Context, resp *teov20220901.DescribeFunctionRulesResponseParams) error { + d := tccommon.ResourceDataFromContext(ctx) + + rulesList := []string{} + if resp.FunctionRules != nil { + for _, functionRules := range resp.FunctionRules { + if functionRules.RuleId != nil { + rulesList = append(rulesList, *functionRules.RuleId) + } + } + + _ = d.Set("rule_ids", rulesList) + } + + return nil +} diff --git a/tencentcloud/services/teo/resource_tc_teo_function_rule_priority_test.go b/tencentcloud/services/teo/resource_tc_teo_function_rule_priority_test.go new file mode 100644 index 0000000000..3536bcde88 --- /dev/null +++ b/tencentcloud/services/teo/resource_tc_teo_function_rule_priority_test.go @@ -0,0 +1,47 @@ +package teo_test + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + + tcacctest "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/acctest" +) + +func TestAccTencentCloudTeoFunctionRulePriorityResource_basic(t *testing.T) { + t.Parallel() + resource.Test(t, resource.TestCase{ + PreCheck: func() { + tcacctest.AccPreCheck(t) + }, + Providers: tcacctest.AccProviders, + Steps: []resource.TestStep{ + { + Config: testAccTeoFunctionRulePriority, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("tencentcloud_teo_function_rule_priority.teo_function_rule_priority", "id"), + resource.TestCheckResourceAttrSet("tencentcloud_teo_function_rule_priority.teo_function_rule_priority", "function_id"), + resource.TestCheckResourceAttrSet("tencentcloud_teo_function_rule_priority.teo_function_rule_priority", "zone_id"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_rule_priority.teo_function_rule_priority", "rule_ids.#", "2"), + ), + }, + { + ResourceName: "tencentcloud_teo_function_rule_priority.teo_function_rule_priority", + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +const testAccTeoFunctionRulePriority = ` + +resource "tencentcloud_teo_function_rule_priority" "teo_function_rule_priority" { + function_id = "ef-txx7fnua" + rule_ids = [ + "rule-equpbht3", + "rule-ax28n3g6", + ] + zone_id = "zone-2qtuhspy7cr6" +} +` diff --git a/tencentcloud/services/teo/resource_tc_teo_function_rule_test.go b/tencentcloud/services/teo/resource_tc_teo_function_rule_test.go new file mode 100644 index 0000000000..c94eae418a --- /dev/null +++ b/tencentcloud/services/teo/resource_tc_teo_function_rule_test.go @@ -0,0 +1,190 @@ +package teo_test + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + + tcacctest "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/acctest" +) + +func TestAccTencentCloudTeoFunctionRuleResource_basic(t *testing.T) { + t.Parallel() + resource.Test(t, resource.TestCase{ + PreCheck: func() { + tcacctest.AccPreCheck(t) + }, + Providers: tcacctest.AccProviders, + Steps: []resource.TestStep{ + { + Config: testAccTeoFunctionRule, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("tencentcloud_teo_function_rule.teo_function_rule", "id"), + resource.TestCheckResourceAttrSet("tencentcloud_teo_function_rule.teo_function_rule", "function_id"), + resource.TestCheckResourceAttrSet("tencentcloud_teo_function_rule.teo_function_rule", "zone_id"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_rule.teo_function_rule", "remark", "aaa"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_rule.teo_function_rule", "function_rule_conditions.#", "2"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_rule.teo_function_rule", "function_rule_conditions.0.rule_conditions.#", "2"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_rule.teo_function_rule", "function_rule_conditions.0.rule_conditions.0.ignore_case", "false"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_rule.teo_function_rule", "function_rule_conditions.0.rule_conditions.0.operator", "equal"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_rule.teo_function_rule", "function_rule_conditions.0.rule_conditions.0.target", "host"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_rule.teo_function_rule", "function_rule_conditions.0.rule_conditions.0.values.#", "1"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_rule.teo_function_rule", "function_rule_conditions.0.rule_conditions.0.values.0", "aaa.makn.cn"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_rule.teo_function_rule", "function_rule_conditions.0.rule_conditions.1.ignore_case", "false"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_rule.teo_function_rule", "function_rule_conditions.0.rule_conditions.1.operator", "equal"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_rule.teo_function_rule", "function_rule_conditions.0.rule_conditions.1.target", "extension"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_rule.teo_function_rule", "function_rule_conditions.0.rule_conditions.1.values.#", "1"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_rule.teo_function_rule", "function_rule_conditions.0.rule_conditions.1.values.0", ".txt"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_rule.teo_function_rule", "function_rule_conditions.1.rule_conditions.#", "2"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_rule.teo_function_rule", "function_rule_conditions.1.rule_conditions.0.ignore_case", "false"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_rule.teo_function_rule", "function_rule_conditions.1.rule_conditions.0.operator", "notequal"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_rule.teo_function_rule", "function_rule_conditions.1.rule_conditions.0.target", "host"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_rule.teo_function_rule", "function_rule_conditions.1.rule_conditions.0.values.#", "1"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_rule.teo_function_rule", "function_rule_conditions.1.rule_conditions.0.values.0", "aaa.makn.cn"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_rule.teo_function_rule", "function_rule_conditions.1.rule_conditions.1.ignore_case", "false"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_rule.teo_function_rule", "function_rule_conditions.1.rule_conditions.1.operator", "equal"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_rule.teo_function_rule", "function_rule_conditions.1.rule_conditions.1.target", "extension"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_rule.teo_function_rule", "function_rule_conditions.1.rule_conditions.1.values.#", "1"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_rule.teo_function_rule", "function_rule_conditions.1.rule_conditions.1.values.0", ".png"), + ), + }, + { + ResourceName: "tencentcloud_teo_function_rule.teo_function_rule", + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccTeoFunctionRuleUp, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("tencentcloud_teo_function_rule.teo_function_rule", "id"), + resource.TestCheckResourceAttrSet("tencentcloud_teo_function_rule.teo_function_rule", "function_id"), + resource.TestCheckResourceAttrSet("tencentcloud_teo_function_rule.teo_function_rule", "zone_id"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_rule.teo_function_rule", "remark", "bbb"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_rule.teo_function_rule", "function_rule_conditions.#", "2"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_rule.teo_function_rule", "function_rule_conditions.0.rule_conditions.#", "2"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_rule.teo_function_rule", "function_rule_conditions.0.rule_conditions.0.ignore_case", "false"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_rule.teo_function_rule", "function_rule_conditions.0.rule_conditions.0.operator", "notequal"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_rule.teo_function_rule", "function_rule_conditions.0.rule_conditions.0.target", "host"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_rule.teo_function_rule", "function_rule_conditions.0.rule_conditions.0.values.#", "1"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_rule.teo_function_rule", "function_rule_conditions.0.rule_conditions.0.values.0", "aaa.makn.cn"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_rule.teo_function_rule", "function_rule_conditions.0.rule_conditions.1.ignore_case", "false"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_rule.teo_function_rule", "function_rule_conditions.0.rule_conditions.1.operator", "equal"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_rule.teo_function_rule", "function_rule_conditions.0.rule_conditions.1.target", "extension"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_rule.teo_function_rule", "function_rule_conditions.0.rule_conditions.1.values.#", "1"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_rule.teo_function_rule", "function_rule_conditions.0.rule_conditions.1.values.0", ".txt"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_rule.teo_function_rule", "function_rule_conditions.1.rule_conditions.#", "2"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_rule.teo_function_rule", "function_rule_conditions.1.rule_conditions.0.ignore_case", "false"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_rule.teo_function_rule", "function_rule_conditions.1.rule_conditions.0.operator", "notequal"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_rule.teo_function_rule", "function_rule_conditions.1.rule_conditions.0.target", "host"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_rule.teo_function_rule", "function_rule_conditions.1.rule_conditions.0.values.#", "1"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_rule.teo_function_rule", "function_rule_conditions.1.rule_conditions.0.values.0", "aaa.makn.cn"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_rule.teo_function_rule", "function_rule_conditions.1.rule_conditions.1.ignore_case", "false"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_rule.teo_function_rule", "function_rule_conditions.1.rule_conditions.1.operator", "equal"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_rule.teo_function_rule", "function_rule_conditions.1.rule_conditions.1.target", "extension"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_rule.teo_function_rule", "function_rule_conditions.1.rule_conditions.1.values.#", "1"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_rule.teo_function_rule", "function_rule_conditions.1.rule_conditions.1.values.0", ".png"), + ), + }, + }, + }) +} + +const testAccTeoFunctionRule = ` + +resource "tencentcloud_teo_function_rule" "teo_function_rule" { + function_id = "ef-txx7fnua" + remark = "aaa" + zone_id = "zone-2qtuhspy7cr6" + + function_rule_conditions { + rule_conditions { + ignore_case = false + name = null + operator = "equal" + target = "host" + values = [ + "aaa.makn.cn", + ] + } + rule_conditions { + ignore_case = false + name = null + operator = "equal" + target = "extension" + values = [ + ".txt", + ] + } + } + function_rule_conditions { + rule_conditions { + ignore_case = false + name = null + operator = "notequal" + target = "host" + values = [ + "aaa.makn.cn", + ] + } + rule_conditions { + ignore_case = false + name = null + operator = "equal" + target = "extension" + values = [ + ".png", + ] + } + } +} +` + +const testAccTeoFunctionRuleUp = ` + +resource "tencentcloud_teo_function_rule" "teo_function_rule" { + function_id = "ef-txx7fnua" + remark = "bbb" + zone_id = "zone-2qtuhspy7cr6" + + function_rule_conditions { + rule_conditions { + ignore_case = false + name = null + operator = "notequal" + target = "host" + values = [ + "aaa.makn.cn", + ] + } + rule_conditions { + ignore_case = false + name = null + operator = "equal" + target = "extension" + values = [ + ".txt", + ] + } + } + function_rule_conditions { + rule_conditions { + ignore_case = false + name = null + operator = "notequal" + target = "host" + values = [ + "aaa.makn.cn", + ] + } + rule_conditions { + ignore_case = false + name = null + operator = "equal" + target = "extension" + values = [ + ".png", + ] + } + } +} +` diff --git a/tencentcloud/services/teo/resource_tc_teo_function_runtime_environment.go b/tencentcloud/services/teo/resource_tc_teo_function_runtime_environment.go new file mode 100644 index 0000000000..4dfbc8d849 --- /dev/null +++ b/tencentcloud/services/teo/resource_tc_teo_function_runtime_environment.go @@ -0,0 +1,329 @@ +// Code generated by iacg; DO NOT EDIT. +package teo + +import ( + "context" + "fmt" + "log" + "strings" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + teov20220901 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/teo/v20220901" + + tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" +) + +func ResourceTencentCloudTeoFunctionRuntimeEnvironment() *schema.Resource { + return &schema.Resource{ + Create: resourceTencentCloudTeoFunctionRuntimeEnvironmentCreate, + Read: resourceTencentCloudTeoFunctionRuntimeEnvironmentRead, + Update: resourceTencentCloudTeoFunctionRuntimeEnvironmentUpdate, + Delete: resourceTencentCloudTeoFunctionRuntimeEnvironmentDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + Schema: map[string]*schema.Schema{ + "zone_id": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + Description: "ID of the site.", + }, + + "function_id": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + Description: "ID of the Function.", + }, + + "environment_variables": { + Type: schema.TypeList, + Required: true, + Description: "The environment variable list.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "key": { + Type: schema.TypeString, + Required: true, + Description: "The name of the variable, which is limited to alphanumeric characters and the special characters `@`, `.`, `-`, and `_`. It can have a maximum of 64 bytes and should not be duplicated.", + }, + "value": { + Type: schema.TypeString, + Required: true, + Description: "The value of the variable, which is limited to a maximum of 5000 bytes. The default value is empty.", + }, + "type": { + Type: schema.TypeString, + Required: true, + Description: "The type of the variable can have the following values: - `string`: Represents a string type. - `json`: Represents a JSON object type.", + }, + }, + }, + }, + }, + } +} + +func resourceTencentCloudTeoFunctionRuntimeEnvironmentCreate(d *schema.ResourceData, meta interface{}) error { + defer tccommon.LogElapsed("resource.tencentcloud_teo_function_runtime_environment.create")() + defer tccommon.InconsistentCheck(d, meta)() + + logId := tccommon.GetLogId(tccommon.ContextNil) + + ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) + + var ( + zoneId string + functionId string + ) + var ( + request = teov20220901.NewHandleFunctionRuntimeEnvironmentRequest() + response = teov20220901.NewHandleFunctionRuntimeEnvironmentResponse() + ) + + if v, ok := d.GetOk("zone_id"); ok { + zoneId = v.(string) + } + if v, ok := d.GetOk("function_id"); ok { + functionId = v.(string) + } + + request.ZoneId = helper.String(zoneId) + + request.FunctionId = helper.String(functionId) + + if v, ok := d.GetOk("environment_variables"); ok { + for _, item := range v.([]interface{}) { + environmentVariablesMap := item.(map[string]interface{}) + functionEnvironmentVariable := teov20220901.FunctionEnvironmentVariable{} + if v, ok := environmentVariablesMap["key"]; ok { + functionEnvironmentVariable.Key = helper.String(v.(string)) + } + if v, ok := environmentVariablesMap["value"]; ok { + functionEnvironmentVariable.Value = helper.String(v.(string)) + } + if v, ok := environmentVariablesMap["type"]; ok { + functionEnvironmentVariable.Type = helper.String(v.(string)) + } + request.EnvironmentVariables = append(request.EnvironmentVariables, &functionEnvironmentVariable) + } + } + + if err := resourceTencentCloudTeoFunctionRuntimeEnvironmentCreatePostFillRequest0(ctx, request); err != nil { + return err + } + + err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseTeoV20220901Client().HandleFunctionRuntimeEnvironmentWithContext(ctx, request) + if e != nil { + return tccommon.RetryError(e) + } else { + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) + } + response = result + return nil + }) + if err != nil { + log.Printf("[CRITAL]%s create teo function runtime environment failed, reason:%+v", logId, err) + return err + } + + _ = response + + d.SetId(strings.Join([]string{zoneId, functionId}, tccommon.FILED_SP)) + + return resourceTencentCloudTeoFunctionRuntimeEnvironmentRead(d, meta) +} + +func resourceTencentCloudTeoFunctionRuntimeEnvironmentRead(d *schema.ResourceData, meta interface{}) error { + defer tccommon.LogElapsed("resource.tencentcloud_teo_function_runtime_environment.read")() + defer tccommon.InconsistentCheck(d, meta)() + + logId := tccommon.GetLogId(tccommon.ContextNil) + + ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) + + service := TeoService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} + + idSplit := strings.Split(d.Id(), tccommon.FILED_SP) + if len(idSplit) != 2 { + return fmt.Errorf("id is broken,%s", d.Id()) + } + zoneId := idSplit[0] + functionId := idSplit[1] + + _ = d.Set("zone_id", zoneId) + + _ = d.Set("function_id", functionId) + + respData, err := service.DescribeTeoFunctionRuntimeEnvironmentById(ctx, zoneId, functionId) + if err != nil { + return err + } + + if respData == nil { + d.SetId("") + log.Printf("[WARN]%s resource `teo_function_runtime_environment` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) + return nil + } + environmentVariablesList := make([]map[string]interface{}, 0, len(respData.EnvironmentVariables)) + if respData.EnvironmentVariables != nil { + for _, environmentVariables := range respData.EnvironmentVariables { + environmentVariablesMap := map[string]interface{}{} + + if environmentVariables.Key != nil { + environmentVariablesMap["key"] = environmentVariables.Key + } + + if environmentVariables.Value != nil { + environmentVariablesMap["value"] = environmentVariables.Value + } + + if environmentVariables.Type != nil { + environmentVariablesMap["type"] = environmentVariables.Type + } + + environmentVariablesList = append(environmentVariablesList, environmentVariablesMap) + } + + _ = d.Set("environment_variables", environmentVariablesList) + } + + return nil +} + +func resourceTencentCloudTeoFunctionRuntimeEnvironmentUpdate(d *schema.ResourceData, meta interface{}) error { + defer tccommon.LogElapsed("resource.tencentcloud_teo_function_runtime_environment.update")() + defer tccommon.InconsistentCheck(d, meta)() + + logId := tccommon.GetLogId(tccommon.ContextNil) + + ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) + + idSplit := strings.Split(d.Id(), tccommon.FILED_SP) + if len(idSplit) != 2 { + return fmt.Errorf("id is broken,%s", d.Id()) + } + zoneId := idSplit[0] + functionId := idSplit[1] + + needChange := false + mutableArgs := []string{"environment_variables"} + for _, v := range mutableArgs { + if d.HasChange(v) { + needChange = true + break + } + } + + if needChange { + request := teov20220901.NewHandleFunctionRuntimeEnvironmentRequest() + + request.ZoneId = helper.String(zoneId) + + request.FunctionId = helper.String(functionId) + + if v, ok := d.GetOk("environment_variables"); ok { + for _, item := range v.([]interface{}) { + environmentVariablesMap := item.(map[string]interface{}) + functionEnvironmentVariable := teov20220901.FunctionEnvironmentVariable{} + if v, ok := environmentVariablesMap["key"]; ok { + functionEnvironmentVariable.Key = helper.String(v.(string)) + } + if v, ok := environmentVariablesMap["value"]; ok { + functionEnvironmentVariable.Value = helper.String(v.(string)) + } + if v, ok := environmentVariablesMap["type"]; ok { + functionEnvironmentVariable.Type = helper.String(v.(string)) + } + request.EnvironmentVariables = append(request.EnvironmentVariables, &functionEnvironmentVariable) + } + } + + if err := resourceTencentCloudTeoFunctionRuntimeEnvironmentUpdatePostFillRequest0(ctx, request); err != nil { + return err + } + + err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseTeoV20220901Client().HandleFunctionRuntimeEnvironmentWithContext(ctx, request) + if e != nil { + return tccommon.RetryError(e) + } else { + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) + } + return nil + }) + if err != nil { + log.Printf("[CRITAL]%s update teo function runtime environment failed, reason:%+v", logId, err) + return err + } + } + + return resourceTencentCloudTeoFunctionRuntimeEnvironmentRead(d, meta) +} + +func resourceTencentCloudTeoFunctionRuntimeEnvironmentDelete(d *schema.ResourceData, meta interface{}) error { + defer tccommon.LogElapsed("resource.tencentcloud_teo_function_runtime_environment.delete")() + defer tccommon.InconsistentCheck(d, meta)() + + logId := tccommon.GetLogId(tccommon.ContextNil) + ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) + + idSplit := strings.Split(d.Id(), tccommon.FILED_SP) + if len(idSplit) != 2 { + return fmt.Errorf("id is broken,%s", d.Id()) + } + zoneId := idSplit[0] + functionId := idSplit[1] + + var ( + request = teov20220901.NewHandleFunctionRuntimeEnvironmentRequest() + response = teov20220901.NewHandleFunctionRuntimeEnvironmentResponse() + ) + + request.ZoneId = helper.String(zoneId) + + request.FunctionId = helper.String(functionId) + + if v, ok := d.GetOk("environment_variables"); ok { + for _, item := range v.([]interface{}) { + environmentVariablesMap := item.(map[string]interface{}) + functionEnvironmentVariable := teov20220901.FunctionEnvironmentVariable{} + if v, ok := environmentVariablesMap["key"]; ok { + functionEnvironmentVariable.Key = helper.String(v.(string)) + } + if v, ok := environmentVariablesMap["value"]; ok { + functionEnvironmentVariable.Value = helper.String(v.(string)) + } + if v, ok := environmentVariablesMap["type"]; ok { + functionEnvironmentVariable.Type = helper.String(v.(string)) + } + request.EnvironmentVariables = append(request.EnvironmentVariables, &functionEnvironmentVariable) + } + } + + if err := resourceTencentCloudTeoFunctionRuntimeEnvironmentDeletePostFillRequest0(ctx, request); err != nil { + return err + } + + err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseTeoV20220901Client().HandleFunctionRuntimeEnvironmentWithContext(ctx, request) + if e != nil { + return tccommon.RetryError(e) + } else { + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) + } + response = result + return nil + }) + if err != nil { + log.Printf("[CRITAL]%s delete teo function runtime environment failed, reason:%+v", logId, err) + return err + } + + _ = response + return nil +} diff --git a/tencentcloud/services/teo/resource_tc_teo_function_runtime_environment.md b/tencentcloud/services/teo/resource_tc_teo_function_runtime_environment.md new file mode 100644 index 0000000000..42bccc5562 --- /dev/null +++ b/tencentcloud/services/teo/resource_tc_teo_function_runtime_environment.md @@ -0,0 +1,29 @@ +Provides a resource to create a teo teo_function_runtime_environment + +Example Usage + +```hcl +resource "tencentcloud_teo_function_runtime_environment" "teo_function_runtime_environment" { + function_id = "ef-txx7fnua" + zone_id = "zone-2qtuhspy7cr6" + + environment_variables { + key = "test-a" + type = "string" + value = "AAA" + } + environment_variables { + key = "test-b" + type = "string" + value = "BBB" + } +} +``` + +Import + +teo teo_function_runtime_environment can be imported using the id, e.g. + +``` +terraform import tencentcloud_teo_function_runtime_environment.teo_function_runtime_environment zone_id#function_id +``` diff --git a/tencentcloud/services/teo/resource_tc_teo_function_runtime_environment_extension.go b/tencentcloud/services/teo/resource_tc_teo_function_runtime_environment_extension.go new file mode 100644 index 0000000000..473d339740 --- /dev/null +++ b/tencentcloud/services/teo/resource_tc_teo_function_runtime_environment_extension.go @@ -0,0 +1,24 @@ +package teo + +import ( + "context" + + teov20220901 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/teo/v20220901" + + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" +) + +func resourceTencentCloudTeoFunctionRuntimeEnvironmentCreatePostFillRequest0(ctx context.Context, req *teov20220901.HandleFunctionRuntimeEnvironmentRequest) error { + req.Operation = helper.String("setEnvironmentVariable") + return nil +} + +func resourceTencentCloudTeoFunctionRuntimeEnvironmentUpdatePostFillRequest0(ctx context.Context, req *teov20220901.HandleFunctionRuntimeEnvironmentRequest) error { + req.Operation = helper.String("resetEnvironmentVariable") + return nil +} + +func resourceTencentCloudTeoFunctionRuntimeEnvironmentDeletePostFillRequest0(ctx context.Context, req *teov20220901.HandleFunctionRuntimeEnvironmentRequest) error { + req.Operation = helper.String("deleteEnvironmentVariable") + return nil +} diff --git a/tencentcloud/services/teo/resource_tc_teo_function_runtime_environment_test.go b/tencentcloud/services/teo/resource_tc_teo_function_runtime_environment_test.go new file mode 100644 index 0000000000..9c03572f8d --- /dev/null +++ b/tencentcloud/services/teo/resource_tc_teo_function_runtime_environment_test.go @@ -0,0 +1,93 @@ +package teo_test + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + + tcacctest "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/acctest" +) + +func TestAccTencentCloudTeoFunctionRuntimeEnvironmentResource_basic(t *testing.T) { + t.Parallel() + resource.Test(t, resource.TestCase{ + PreCheck: func() { + tcacctest.AccPreCheck(t) + }, + Providers: tcacctest.AccProviders, + Steps: []resource.TestStep{ + { + Config: testAccTeoFunctionRuntimeEnvironment, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("tencentcloud_teo_function_runtime_environment.teo_function_runtime_environment", "id"), + resource.TestCheckResourceAttrSet("tencentcloud_teo_function_runtime_environment.teo_function_runtime_environment", "function_id"), + resource.TestCheckResourceAttrSet("tencentcloud_teo_function_runtime_environment.teo_function_runtime_environment", "zone_id"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_runtime_environment.teo_function_runtime_environment", "environment_variables.#", "2"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_runtime_environment.teo_function_runtime_environment", "environment_variables.0.key", "test-a"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_runtime_environment.teo_function_runtime_environment", "environment_variables.0.type", "string"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_runtime_environment.teo_function_runtime_environment", "environment_variables.0.value", "AAA"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_runtime_environment.teo_function_runtime_environment", "environment_variables.1.key", "test-b"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_runtime_environment.teo_function_runtime_environment", "environment_variables.1.type", "string"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_runtime_environment.teo_function_runtime_environment", "environment_variables.1.value", "BBB"), + ), + }, + { + ResourceName: "tencentcloud_teo_function_runtime_environment.teo_function_runtime_environment", + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccTeoFunctionRuntimeEnvironmentUp, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("tencentcloud_teo_function_runtime_environment.teo_function_runtime_environment", "id"), + resource.TestCheckResourceAttrSet("tencentcloud_teo_function_runtime_environment.teo_function_runtime_environment", "function_id"), + resource.TestCheckResourceAttrSet("tencentcloud_teo_function_runtime_environment.teo_function_runtime_environment", "zone_id"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_runtime_environment.teo_function_runtime_environment", "environment_variables.#", "2"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_runtime_environment.teo_function_runtime_environment", "environment_variables.0.key", "test-b"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_runtime_environment.teo_function_runtime_environment", "environment_variables.0.type", "string"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_runtime_environment.teo_function_runtime_environment", "environment_variables.0.value", "BBB"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_runtime_environment.teo_function_runtime_environment", "environment_variables.1.key", "test-a"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_runtime_environment.teo_function_runtime_environment", "environment_variables.1.type", "string"), + resource.TestCheckResourceAttr("tencentcloud_teo_function_runtime_environment.teo_function_runtime_environment", "environment_variables.1.value", "AAA"), + ), + }, + }, + }) +} + +const testAccTeoFunctionRuntimeEnvironment = ` + +resource "tencentcloud_teo_function_runtime_environment" "teo_function_runtime_environment" { + function_id = "ef-txx7fnua" + zone_id = "zone-2qtuhspy7cr6" + + environment_variables { + key = "test-a" + type = "string" + value = "AAA" + } + environment_variables { + key = "test-b" + type = "string" + value = "BBB" + } +} +` +const testAccTeoFunctionRuntimeEnvironmentUp = ` + +resource "tencentcloud_teo_function_runtime_environment" "teo_function_runtime_environment" { + function_id = "ef-txx7fnua" + zone_id = "zone-2qtuhspy7cr6" + + environment_variables { + key = "test-b" + type = "string" + value = "BBB" + } + environment_variables { + key = "test-a" + type = "string" + value = "AAA" + } +} +` diff --git a/tencentcloud/services/teo/resource_tc_teo_function_test.go b/tencentcloud/services/teo/resource_tc_teo_function_test.go new file mode 100644 index 0000000000..d13397a72b --- /dev/null +++ b/tencentcloud/services/teo/resource_tc_teo_function_test.go @@ -0,0 +1,81 @@ +package teo_test + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + + tcacctest "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/acctest" +) + +func TestAccTencentCloudTeoFunctionResource_basic(t *testing.T) { + t.Parallel() + resource.Test(t, resource.TestCase{ + PreCheck: func() { + tcacctest.AccPreCheck(t) + }, + Providers: tcacctest.AccProviders, + Steps: []resource.TestStep{ + { + Config: testAccTeoFunction, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("tencentcloud_teo_function.teo_function", "id"), + resource.TestCheckResourceAttr("tencentcloud_teo_function.teo_function", "name", "aaa-zone-2qtuhspy7cr6-1310708577"), + resource.TestCheckResourceAttr("tencentcloud_teo_function.teo_function", "remark", "test"), + resource.TestCheckResourceAttr("tencentcloud_teo_function.teo_function", "content", `addEventListener('fetch', e => { + const response = new Response('Hello World!!'); + e.respondWith(response); +}); +`), + ), + }, + { + ResourceName: "tencentcloud_teo_function.teo_function", + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccTeoFunctionUp, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("tencentcloud_teo_function.teo_function", "id"), + resource.TestCheckResourceAttr("tencentcloud_teo_function.teo_function", "name", "aaa-zone-2qtuhspy7cr6-1310708577"), + resource.TestCheckResourceAttr("tencentcloud_teo_function.teo_function", "remark", "test-update"), + resource.TestCheckResourceAttr("tencentcloud_teo_function.teo_function", "content", `addEventListener('fetch', e => { + const response = new Response('Hello World'); + e.respondWith(response); +}); +`), + ), + }, + }, + }) +} + +const testAccTeoFunction = ` + +resource "tencentcloud_teo_function" "teo_function" { + content = <<-EOT + addEventListener('fetch', e => { + const response = new Response('Hello World!!'); + e.respondWith(response); + }); + EOT + name = "aaa-zone-2qtuhspy7cr6-1310708577" + remark = "test" + zone_id = "zone-2qtuhspy7cr6" +} +` +const testAccTeoFunctionUp = ` + +resource "tencentcloud_teo_function" "teo_function" { + content = <<-EOT + addEventListener('fetch', e => { + const response = new Response('Hello World'); + e.respondWith(response); + }); + EOT + name = "aaa-zone-2qtuhspy7cr6-1310708577" + remark = "test-update" + zone_id = "zone-2qtuhspy7cr6" +} +` diff --git a/tencentcloud/services/teo/service_tencentcloud_teo.go b/tencentcloud/services/teo/service_tencentcloud_teo.go index 44d6d3287b..c418805c11 100644 --- a/tencentcloud/services/teo/service_tencentcloud_teo.go +++ b/tencentcloud/services/teo/service_tencentcloud_teo.go @@ -1164,3 +1164,125 @@ func (me *TeoService) DescribeTeoSecurityIpGroupById(ctx context.Context, zoneId ret = response.Response return } + +func (me *TeoService) DescribeTeoFunctionById(ctx context.Context, zoneId string, functionId string) (ret *teo.Function, errRet error) { + logId := tccommon.GetLogId(ctx) + + request := teo.NewDescribeFunctionsRequest() + request.ZoneId = helper.String(zoneId) + request.FunctionIds = []*string{helper.String(functionId)} + + defer func() { + if errRet != nil { + log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", logId, request.GetAction(), request.ToJsonString(), errRet.Error()) + } + }() + + ratelimit.Check(request.GetAction()) + + response, err := me.client.UseTeoV20220901Client().DescribeFunctions(request) + if err != nil { + errRet = err + return + } + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), response.ToJsonString()) + + if len(response.Response.Functions) < 1 { + return + } + + ret = response.Response.Functions[0] + return +} + +func (me *TeoService) DescribeTeoFunctionRuleById(ctx context.Context, zoneId string, functionId string, ruleId string) (ret *teo.FunctionRule, errRet error) { + logId := tccommon.GetLogId(ctx) + + request := teo.NewDescribeFunctionRulesRequest() + request.ZoneId = helper.String(zoneId) + request.Filters = []*teo.Filter{{ + Name: helper.String("function-id"), + Values: []*string{helper.String(functionId)}, + }, { + Name: helper.String("rule-id"), + Values: []*string{helper.String(ruleId)}, + }} + + defer func() { + if errRet != nil { + log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", logId, request.GetAction(), request.ToJsonString(), errRet.Error()) + } + }() + + ratelimit.Check(request.GetAction()) + + response, err := me.client.UseTeoV20220901Client().DescribeFunctionRules(request) + if err != nil { + errRet = err + return + } + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), response.ToJsonString()) + + if len(response.Response.FunctionRules) < 1 { + return + } + + ret = response.Response.FunctionRules[0] + return +} + +func (me *TeoService) DescribeTeoFunctionRulePriorityById(ctx context.Context, zoneId string, functionId string) (ret *teo.DescribeFunctionRulesResponseParams, errRet error) { + logId := tccommon.GetLogId(ctx) + + request := teo.NewDescribeFunctionRulesRequest() + request.ZoneId = helper.String(zoneId) + filter := &teo.Filter{ + Name: helper.String("function-id"), + Values: []*string{helper.String(functionId)}, + } + request.Filters = append(request.Filters, filter) + + defer func() { + if errRet != nil { + log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", logId, request.GetAction(), request.ToJsonString(), errRet.Error()) + } + }() + + ratelimit.Check(request.GetAction()) + + response, err := me.client.UseTeoV20220901Client().DescribeFunctionRules(request) + if err != nil { + errRet = err + return + } + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), response.ToJsonString()) + + ret = response.Response + return +} + +func (me *TeoService) DescribeTeoFunctionRuntimeEnvironmentById(ctx context.Context, zoneId string, functionId string) (ret *teo.DescribeFunctionRuntimeEnvironmentResponseParams, errRet error) { + logId := tccommon.GetLogId(ctx) + + request := teo.NewDescribeFunctionRuntimeEnvironmentRequest() + request.ZoneId = helper.String(zoneId) + request.FunctionId = helper.String(functionId) + + defer func() { + if errRet != nil { + log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", logId, request.GetAction(), request.ToJsonString(), errRet.Error()) + } + }() + + ratelimit.Check(request.GetAction()) + + response, err := me.client.UseTeoV20220901Client().DescribeFunctionRuntimeEnvironment(request) + if err != nil { + errRet = err + return + } + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), response.ToJsonString()) + + ret = response.Response + return +} diff --git a/website/docs/r/teo_function.html.markdown b/website/docs/r/teo_function.html.markdown new file mode 100644 index 0000000000..a8868fc836 --- /dev/null +++ b/website/docs/r/teo_function.html.markdown @@ -0,0 +1,57 @@ +--- +subcategory: "TencentCloud EdgeOne(TEO)" +layout: "tencentcloud" +page_title: "TencentCloud: tencentcloud_teo_function" +sidebar_current: "docs-tencentcloud-resource-teo_function" +description: |- + Provides a resource to create a teo teo_function +--- + +# tencentcloud_teo_function + +Provides a resource to create a teo teo_function + +## Example Usage + +```hcl +resource "tencentcloud_teo_function" "teo_function" { + content = <<-EOT + addEventListener('fetch', e => { + const response = new Response('Hello World!!'); + e.respondWith(response); + }); + EOT + name = "aaa-zone-2qtuhspy7cr6-1310708577" + remark = "test" + zone_id = "zone-2qtuhspy7cr6" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `content` - (Required, String) Function content, currently only supports JavaScript code, with a maximum size of 5MB. +* `name` - (Required, String) Function name. It can only contain lowercase letters, numbers, hyphens, must start and end with a letter or number, and can have a maximum length of 30 characters. +* `zone_id` - (Required, String, ForceNew) ID of the site. +* `remark` - (Optional, String) Function description, maximum support of 60 characters. + +## Attributes Reference + +In addition to all arguments above, the following attributes are exported: + +* `id` - ID of the resource. +* `create_time` - Creation time. The time is in Coordinated Universal Time (UTC) and follows the date and time format specified by the ISO 8601 standard. +* `domain` - The default domain name for the function. +* `function_id` - ID of the Function. +* `update_time` - Modification time. The time is in Coordinated Universal Time (UTC) and follows the date and time format specified by the ISO 8601 standard. + + +## Import + +teo teo_function can be imported using the id, e.g. + +``` +terraform import tencentcloud_teo_function.teo_function zone_id#function_id +``` + diff --git a/website/docs/r/teo_function_rule.html.markdown b/website/docs/r/teo_function_rule.html.markdown new file mode 100644 index 0000000000..014cdf7ef3 --- /dev/null +++ b/website/docs/r/teo_function_rule.html.markdown @@ -0,0 +1,126 @@ +--- +subcategory: "TencentCloud EdgeOne(TEO)" +layout: "tencentcloud" +page_title: "TencentCloud: tencentcloud_teo_function_rule" +sidebar_current: "docs-tencentcloud-resource-teo_function_rule" +description: |- + Provides a resource to create a teo teo_function_rule +--- + +# tencentcloud_teo_function_rule + +Provides a resource to create a teo teo_function_rule + +## Example Usage + +```hcl +resource "tencentcloud_teo_function_rule" "teo_function_rule" { + function_id = "ef-txx7fnua" + remark = "aaa" + zone_id = "zone-2qtuhspy7cr6" + + function_rule_conditions { + rule_conditions { + ignore_case = false + name = null + operator = "equal" + target = "host" + values = [ + "aaa.makn.cn", + ] + } + rule_conditions { + ignore_case = false + name = null + operator = "equal" + target = "extension" + values = [ + ".txt", + ] + } + } + function_rule_conditions { + rule_conditions { + ignore_case = false + name = null + operator = "notequal" + target = "host" + values = [ + "aaa.makn.cn", + ] + } + rule_conditions { + ignore_case = false + name = null + operator = "equal" + target = "extension" + values = [ + ".png", + ] + } + } +} +``` + +## Argument Reference + +The following arguments are supported: + +* `function_id` - (Required, String, ForceNew) ID of the Function. +* `function_rule_conditions` - (Required, List) The list of rule conditions, where the conditions are connected by an "OR" relationship. +* `zone_id` - (Required, String, ForceNew) ID of the site. +* `remark` - (Optional, String) Rule description, maximum support of 60 characters. + +The `function_rule_conditions` object supports the following: + +* `rule_conditions` - (Required, List) For edge function trigger rule conditions, if all items in the list are satisfied, then the condition is considered fulfilled. + +The `rule_conditions` object of `function_rule_conditions` supports the following: + +* `operator` - (Required, String) Operator. Valid values: + - `equals`: Equals. + - `notEquals`: Does not equal. + - `exist`: Exists. + - `notexist`: Does not exist. +* `target` - (Required, String) The match type. Values: + - `filename`: File name. + - `extension`: File extension. + - `host`: Host. + - `full_url`: Full URL, which indicates the complete URL path under the current site and must contain the HTTP protocol, host, and path. + - `url`: Partial URL under the current site. + - `client_country`: Country/Region of the client. + - `query_string`: Query string in the request URL. + - `request_header`: HTTP request header. +* `ignore_case` - (Optional, Bool) Whether the parameter value is case insensitive. Default value: false. +* `name` - (Optional, String) The parameter name of the match type. This field is required only when `Target=query_string/request_header`. + - `query_string`: Name of the query string, such as "lang" and "version" in "lang=cn&version=1". + - `request_header`: Name of the HTTP request header, such as "Accept-Language" in the "Accept-Language:zh-CN,zh;q=0.9" header. +* `values` - (Optional, Set) The parameter value of the match type. It can be an empty string only when `Target=query string/request header` and `Operator=exist/notexist`. + - When `Target=extension`, enter the file extension, such as "jpg" and "txt". + - When `Target=filename`, enter the file name, such as "foo" in "foo.jpg". + - When `Target=all`, it indicates any site request. + - When `Target=host`, enter the host under the current site, such as "www.maxx55.com". + - When `Target=url`, enter the partial URL path under the current site, such as "/example". + - When `Target=full_url`, enter the complete URL under the current site. It must contain the HTTP protocol, host, and path, such as "https://www.maxx55.cn/example". + - When `Target=client_country`, enter the ISO-3166 country/region code. + - When `Target=query_string`, enter the value of the query string, such as "cn" and "1" in "lang=cn&version=1". + - When `Target=request_header`, enter the HTTP request header value, such as "zh-CN,zh;q=0.9" in the "Accept-Language:zh-CN,zh;q=0.9" header. + +## Attributes Reference + +In addition to all arguments above, the following attributes are exported: + +* `id` - ID of the resource. +* `function_name` - The name of the function. +* `priority` - The priority of the function trigger rule. A higher numerical value indicates a higher priority. +* `rule_id` - ID of the Function Rule. + + +## Import + +teo teo_function_rule can be imported using the id, e.g. + +``` +terraform import tencentcloud_teo_function_rule.teo_function_rule zone_id#function_id#rule_id +``` + diff --git a/website/docs/r/teo_function_rule_priority.html.markdown b/website/docs/r/teo_function_rule_priority.html.markdown new file mode 100644 index 0000000000..f322f0199b --- /dev/null +++ b/website/docs/r/teo_function_rule_priority.html.markdown @@ -0,0 +1,50 @@ +--- +subcategory: "TencentCloud EdgeOne(TEO)" +layout: "tencentcloud" +page_title: "TencentCloud: tencentcloud_teo_function_rule_priority" +sidebar_current: "docs-tencentcloud-resource-teo_function_rule_priority" +description: |- + Provides a resource to create a teo teo_function_rule_priority +--- + +# tencentcloud_teo_function_rule_priority + +Provides a resource to create a teo teo_function_rule_priority + +## Example Usage + +```hcl +resource "tencentcloud_teo_function_rule_priority" "teo_function_rule_priority" { + function_id = "ef-txx7fnua" + rule_ids = [ + "rule-equpbht3", + "rule-ax28n3g6", + ] + zone_id = "zone-2qtuhspy7cr6" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `function_id` - (Required, String, ForceNew) ID of the Function. +* `rule_ids` - (Required, List: [`String`]) he list of rule IDs. It is required to include all rule IDs after adjusting their priorities. The execution order of multiple rules follows a top-down sequence. If not specified, the original priority order will be maintained. +* `zone_id` - (Required, String, ForceNew) ID of the site. + +## Attributes Reference + +In addition to all arguments above, the following attributes are exported: + +* `id` - ID of the resource. + + + +## Import + +teo teo_function_rule_priority can be imported using the id, e.g. + +``` +terraform import tencentcloud_teo_function_rule_priority.teo_function_rule_priority zone_id#function_id +``` + diff --git a/website/docs/r/teo_function_runtime_environment.html.markdown b/website/docs/r/teo_function_runtime_environment.html.markdown new file mode 100644 index 0000000000..ca0c1c3dd7 --- /dev/null +++ b/website/docs/r/teo_function_runtime_environment.html.markdown @@ -0,0 +1,63 @@ +--- +subcategory: "TencentCloud EdgeOne(TEO)" +layout: "tencentcloud" +page_title: "TencentCloud: tencentcloud_teo_function_runtime_environment" +sidebar_current: "docs-tencentcloud-resource-teo_function_runtime_environment" +description: |- + Provides a resource to create a teo teo_function_runtime_environment +--- + +# tencentcloud_teo_function_runtime_environment + +Provides a resource to create a teo teo_function_runtime_environment + +## Example Usage + +```hcl +resource "tencentcloud_teo_function_runtime_environment" "teo_function_runtime_environment" { + function_id = "ef-txx7fnua" + zone_id = "zone-2qtuhspy7cr6" + + environment_variables { + key = "test-a" + type = "string" + value = "AAA" + } + environment_variables { + key = "test-b" + type = "string" + value = "BBB" + } +} +``` + +## Argument Reference + +The following arguments are supported: + +* `environment_variables` - (Required, List) The environment variable list. +* `function_id` - (Required, String, ForceNew) ID of the Function. +* `zone_id` - (Required, String, ForceNew) ID of the site. + +The `environment_variables` object supports the following: + +* `key` - (Required, String) The name of the variable, which is limited to alphanumeric characters and the special characters `@`, `.`, `-`, and `_`. It can have a maximum of 64 bytes and should not be duplicated. +* `type` - (Required, String) The type of the variable can have the following values: - `string`: Represents a string type. - `json`: Represents a JSON object type. +* `value` - (Required, String) The value of the variable, which is limited to a maximum of 5000 bytes. The default value is empty. + +## Attributes Reference + +In addition to all arguments above, the following attributes are exported: + +* `id` - ID of the resource. + + + +## Import + +teo teo_function_runtime_environment can be imported using the id, e.g. + +``` +terraform import tencentcloud_teo_function_runtime_environment.teo_function_runtime_environment zone_id#function_id +``` + diff --git a/website/tencentcloud.erb b/website/tencentcloud.erb index b8fae6e665..a3a22f8e2e 100644 --- a/website/tencentcloud.erb +++ b/website/tencentcloud.erb @@ -5028,6 +5028,18 @@