diff --git a/cdnetworks/model/domain.go b/cdnetworks/model/domain.go index 226dc79..8777fe0 100644 --- a/cdnetworks/model/domain.go +++ b/cdnetworks/model/domain.go @@ -58,6 +58,10 @@ var DomainSchema = schema.Schema{ stringplanmodifier.UseStateForUnknown(), }, }, + "config_form_id": &schema.StringAttribute{ + Description: "Define the config template to be used for both fs and ca. It will have separated ids that are provided by vendor.", + Optional: true, + }, "accelerate_no_china": &schema.StringAttribute{ Description: "Define is domains is created for mainland or oversea. Default: false", Optional: true, @@ -100,6 +104,7 @@ var DomainSchema = schema.Schema{ }, "origin_config": &schema.SingleNestedAttribute{ Description: "Back-to-origin policy setting, which is used to set the origin site information and the back-to-origin policy of the accelerated domain name", + Required: true, Attributes: map[string]schema.Attribute{ "origin_ips": schema.ListAttribute{ ElementType: types.StringType, @@ -115,7 +120,6 @@ var DomainSchema = schema.Schema{ Computed: true, }, }, - Required: true, }, "control_group": &schema.SingleNestedAttribute{ Description: "Update the specific control group. Binding cdn domains to group represent that it belongs to specific account.", @@ -148,6 +152,14 @@ var DomainSchema = schema.Schema{ }, }, }, + "cache_host": &schema.StringAttribute{ + Description: "Targeted domain host to share cache from specific CDN.", + Optional: true, + Computed: true, + PlanModifiers: []planmodifier.String{ + stringplanmodifier.UseStateForUnknown(), + }, + }, }, } @@ -162,6 +174,7 @@ type DomainResourceModel struct { Cname types.String `tfsdk:"cname"` Comment types.String `tfsdk:"comment"` Status types.String `tfsdk:"status"` + ConfigFormId types.String `tfsdk:"config_form_id"` AccelerateNoChina types.String `tfsdk:"accelerate_no_china"` ContractId types.String `tfsdk:"contract_id"` ItemId types.String `tfsdk:"item_id"` @@ -171,6 +184,7 @@ type DomainResourceModel struct { ServiceType types.String `tfsdk:"service_type"` OriginConfig types.Object `tfsdk:"origin_config"` ControlGroup *ControlGroupModel `tfsdk:"control_group"` + CacheHost types.String `tfsdk:"cache_host"` } type ControlGroupModel struct { @@ -212,6 +226,7 @@ func (model *DomainResourceModel) UpdateDomainFromApiConfig(ctx context.Context, model.CdnServiceStatus = types.StringPointerValue(config.CdnServiceStatus) model.HeaderOfClientIp = types.StringPointerValue(config.HeaderOfClientIp) model.Enabled = types.BoolPointerValue(config.Enabled) + model.CacheHost = types.StringPointerValue(config.CacheHost) if config.OriginConfig != nil { defaultOriginHeader := model.Domain if config.OriginConfig.DefaultOriginHostHeader != nil { diff --git a/cdnetworks/resource_content_acceleration_domain.go b/cdnetworks/resource_content_acceleration_domain.go index 4588053..7b765e5 100644 --- a/cdnetworks/resource_content_acceleration_domain.go +++ b/cdnetworks/resource_content_acceleration_domain.go @@ -56,6 +56,7 @@ func (r *contentAccelerationDomainResource) Create(ctx context.Context, req reso addCdnDomainRequest := cdnetworksapi.AddCdnDomainRequest{ Version: API_VERSION, DomainName: model.Domain.ValueStringPointer(), + ConfigFormId: model.ConfigFormId.ValueStringPointer(), AccelerateNoChina: model.AccelerateNoChina.ValueStringPointer(), ContractId: model.ContractId.ValueStringPointer(), ItemId: model.ItemId.ValueStringPointer(), @@ -90,6 +91,20 @@ func (r *contentAccelerationDomainResource) Create(ctx context.Context, req reso return } + // Only trigger UpdateCdnDomain() when cache_host is not "". + if model.CacheHost.ValueString() != "" { + updateCdnDomainRequest := cdnetworksapi.UpdateCdnDomainRequest{ + Version: API_VERSION, + DomainName: model.Domain.ValueStringPointer(), + CacheHost: model.CacheHost.ValueStringPointer(), + } + _, err := r.client.UpdateCdnDomain(model.DomainId.ValueString(), updateCdnDomainRequest) + if err != nil { + resp.Diagnostics.AddError("[API ERROR] Fail to Update Flood Shield Cache-host for Domain", err.Error()) + return + } + } + // Required as copying computedFields from queryResponse. queryCdnDomainResponse, err := r.client.QueryCdnDomain(model.DomainId.ValueString()) if err != nil { @@ -123,7 +138,7 @@ func (r *contentAccelerationDomainResource) Read(ctx context.Context, req resour resp.Diagnostics.AddWarning("[Call API] Trying to bind Content Acceleration Domain to Control Group.", fmt.Sprintf("Domain: %s", model.Domain.ValueString())) // Bind CDN domains to ControlGroup, in case previous bind action doesn't complete. // Prevent error from Read(), Create() might failed to bind into controlGroup. - common.BindCdnDomainToControlGroup(r.client, model) + err = common.BindCdnDomainToControlGroup(r.client, model) if err != nil { return backoff.Permanent(fmt.Errorf("bind control group API error. err: %v", err)) } @@ -165,6 +180,7 @@ func (r *contentAccelerationDomainResource) Update(ctx context.Context, req reso Version: API_VERSION, DomainName: plan.Domain.ValueStringPointer(), Comment: plan.Comment.ValueStringPointer(), + CacheHost: plan.CacheHost.ValueStringPointer(), HeaderOfClientIp: plan.HeaderOfClientIp.ValueStringPointer(), OriginConfig: plan.BuildApiOriginConfig(), } diff --git a/cdnetworks/resource_flood_shield_domain.go b/cdnetworks/resource_flood_shield_domain.go index cfe390a..4e0f80b 100644 --- a/cdnetworks/resource_flood_shield_domain.go +++ b/cdnetworks/resource_flood_shield_domain.go @@ -56,6 +56,7 @@ func (r *floodShieldDomainResource) Create(ctx context.Context, req resource.Cre addCdnDomainRequest := cdnetworksapi.AddCdnDomainRequest{ Version: API_VERSION, DomainName: model.Domain.ValueStringPointer(), + ConfigFormId: model.ConfigFormId.ValueStringPointer(), AccelerateNoChina: model.AccelerateNoChina.ValueStringPointer(), ContractId: model.ContractId.ValueStringPointer(), ItemId: model.ItemId.ValueStringPointer(), @@ -90,6 +91,20 @@ func (r *floodShieldDomainResource) Create(ctx context.Context, req resource.Cre return } + // Only trigger UpdateCdnDomain() when cache_host is not "". + if model.CacheHost.ValueString() != "" { + updateCdnDomainRequest := cdnetworksapi.UpdateCdnDomainRequest{ + Version: API_VERSION, + DomainName: model.Domain.ValueStringPointer(), + CacheHost: model.CacheHost.ValueStringPointer(), + } + _, err := r.client.UpdateCdnDomain(model.DomainId.ValueString(), updateCdnDomainRequest) + if err != nil { + resp.Diagnostics.AddError("[API ERROR] Fail to Update Flood Shield Cache-host for Domain", err.Error()) + return + } + } + // Required as copying computedFields from queryResponse. queryCdnDomainResponse, err := r.client.QueryCdnDomain(model.DomainId.ValueString()) if err != nil { @@ -123,7 +138,7 @@ func (r *floodShieldDomainResource) Read(ctx context.Context, req resource.ReadR resp.Diagnostics.AddWarning("[Call API] Trying to bind CDN Domain to Control Group.", fmt.Sprintf("Domain: %s", model.Domain.ValueString())) // Bind CDN domains to ControlGroup, in case previous bind action doesn't complete. // Prevent error from Read(), Create() might failed to bind into controlGroup. - common.BindCdnDomainToControlGroup(r.client, model) + err = common.BindCdnDomainToControlGroup(r.client, model) if err != nil { return backoff.Permanent(fmt.Errorf("bind control group API error. err: %v", err)) } @@ -165,6 +180,7 @@ func (r *floodShieldDomainResource) Update(ctx context.Context, req resource.Upd Version: API_VERSION, DomainName: plan.Domain.ValueStringPointer(), Comment: plan.Comment.ValueStringPointer(), + CacheHost: plan.CacheHost.ValueStringPointer(), HeaderOfClientIp: plan.HeaderOfClientIp.ValueStringPointer(), OriginConfig: plan.BuildApiOriginConfig(), } diff --git a/cdnetworksapi/domain_management.go b/cdnetworksapi/domain_management.go index 5852556..e820e86 100644 --- a/cdnetworksapi/domain_management.go +++ b/cdnetworksapi/domain_management.go @@ -19,6 +19,7 @@ type CacheBehavior struct { type AddCdnDomainRequest struct { Version string `json:"version,omitempty" xml:"version,omitempty"` DomainName *string `json:"domain-name,omitempty" xml:"domain-name,omitempty"` + ConfigFormId *string `json:"config-form-id,omitempty" xml:"config-form-id,omitempty"` AccelerateNoChina *string `json:"accelerate-no-china,omitempty" xml:"accelerate-no-china,omitempty"` ContractId *string `json:"contract-id,omitempty" xml:"contract-id,omitempty"` ItemId *string `json:"item-id,omitempty" xml:"item-id,omitempty"`