Skip to content

Commit

Permalink
Add config_form_id and cache_host settings. (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
happyjian97 authored Aug 20, 2024
1 parent c6db3df commit 182c836
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
17 changes: 16 additions & 1 deletion cdnetworks/model/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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.",
Expand Down Expand Up @@ -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(),
},
},
},
}

Expand All @@ -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"`
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
18 changes: 17 additions & 1 deletion cdnetworks/resource_content_acceleration_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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))
}
Expand Down Expand Up @@ -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(),
}
Expand Down
18 changes: 17 additions & 1 deletion cdnetworks/resource_flood_shield_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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))
}
Expand Down Expand Up @@ -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(),
}
Expand Down
1 change: 1 addition & 0 deletions cdnetworksapi/domain_management.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down

0 comments on commit 182c836

Please sign in to comment.