Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

CDN-7569 transfer to new originGroup structure #48

Merged
merged 1 commit into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions docs/resources/cdn_origingroup.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,3 @@ Optional:
- `backup` (Boolean) true — The option is active. The origin will not be used until one of active origins become unavailable. false — The option is disabled.
- `enabled` (Boolean) The setting allows to enable or disable an Origin source in the Origins group

Read-Only:

- `id` (Number) The ID of this resource.
19 changes: 7 additions & 12 deletions gcore/resource_gcore_cdn_origin_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,6 @@ func resourceCDNOriginGroup() *schema.Resource {
Computed: true,
Description: "true — The option is active. The origin will not be used until one of active origins become unavailable. false — The option is disabled.",
},
"id": {
Type: schema.TypeInt,
Computed: true,
},
},
},
},
Expand All @@ -77,7 +73,7 @@ func resourceCDNOriginGroupCreate(ctx context.Context, d *schema.ResourceData, m
var req origingroups.GroupRequest
req.Name = d.Get("name").(string)
req.UseNext = d.Get("use_next").(bool)
req.Origins = setToOriginRequests(d.Get("origin").(*schema.Set))
req.Sources = setToSourceRequests(d.Get("origin").(*schema.Set))

result, err := client.OriginGroups().Create(ctx, &req)
if err != nil {
Expand Down Expand Up @@ -109,7 +105,7 @@ func resourceCDNOriginGroupRead(ctx context.Context, d *schema.ResourceData, m i

d.Set("name", result.Name)
d.Set("use_next", result.UseNext)
if err := d.Set("origin", originsToSet(result.Origins)); err != nil {
if err := d.Set("origin", originsToSet(result.Sources)); err != nil {
return diag.FromErr(err)
}

Expand All @@ -131,7 +127,7 @@ func resourceCDNOriginGroupUpdate(ctx context.Context, d *schema.ResourceData, m
var req origingroups.GroupRequest
req.Name = d.Get("name").(string)
req.UseNext = d.Get("use_next").(bool)
req.Origins = setToOriginRequests(d.Get("origin").(*schema.Set))
req.Sources = setToSourceRequests(d.Get("origin").(*schema.Set))

if _, err := client.OriginGroups().Update(ctx, id, &req); err != nil {
return diag.FromErr(err)
Expand Down Expand Up @@ -163,9 +159,9 @@ func resourceCDNOriginGroupDelete(ctx context.Context, d *schema.ResourceData, m
return nil
}

func setToOriginRequests(s *schema.Set) (origins []origingroups.OriginRequest) {
func setToSourceRequests(s *schema.Set) (origins []origingroups.SourceRequest) {
for _, fields := range s.List() {
var originReq origingroups.OriginRequest
var originReq origingroups.SourceRequest

for key, val := range fields.(map[string]interface{}) {
switch key {
Expand All @@ -184,12 +180,11 @@ func setToOriginRequests(s *schema.Set) (origins []origingroups.OriginRequest) {
return origins
}

func originsToSet(origins []origingroups.Origin) *schema.Set {
func originsToSet(origins []origingroups.Source) *schema.Set {
s := &schema.Set{F: originSetIDFunc}

for _, origin := range origins {
fields := make(map[string]interface{})
fields["id"] = origin.ID
fields["source"] = origin.Source
fields["enabled"] = origin.Enabled
fields["backup"] = origin.Backup
Expand All @@ -204,7 +199,7 @@ func originSetIDFunc(i interface{}) int {
fields := i.(map[string]interface{})
h := md5.New()

key := fmt.Sprintf("%d-%s-%t-%t", fields["id"], fields["source"], fields["enabled"], fields["backup"])
key := fmt.Sprintf("%d-%s-%t-%t", fields["source"], fields["enabled"], fields["backup"])
log.Printf("[DEBUG] Origin Set ID = %s\n", key)

io.WriteString(h, key)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/AlekSi/pointer v1.2.0
github.com/G-Core/gcore-dns-sdk-go v0.2.7-0.20230801110428-99ef24b50d4d
github.com/G-Core/gcore-storage-sdk-go v0.1.34
github.com/G-Core/gcorelabscdn-go v0.1.32
github.com/G-Core/gcorelabscdn-go v1.0.0
github.com/G-Core/gcorelabscloud-go v0.6.2
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
github.com/hashicorp/terraform-plugin-sdk/v2 v2.27.0
Expand Down
5 changes: 2 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ github.com/G-Core/gcore-dns-sdk-go v0.2.7-0.20230801110428-99ef24b50d4d h1:nqduB
github.com/G-Core/gcore-dns-sdk-go v0.2.7-0.20230801110428-99ef24b50d4d/go.mod h1:KliUjfPonDvXyAGNiuO+MYVG/7lmWHZ+4Hi0sPxgOjg=
github.com/G-Core/gcore-storage-sdk-go v0.1.34 h1:0GPQfz1kA6mQi6fiisGsh0Um4H9PZeHWIPsc825cDrY=
github.com/G-Core/gcore-storage-sdk-go v0.1.34/go.mod h1:BUAEZZZJJt/+luRFunqziv3+JnbVMLbQXDWz9kV8Te8=
github.com/G-Core/gcorelabscdn-go v0.1.32 h1:VGSmNdoW3lZm5lNf5lPpcO4lSdJYZIltxWkwfxlfodA=
github.com/G-Core/gcorelabscdn-go v0.1.32/go.mod h1:iSGXaTvZBzDHQW+rKFS918BgFVpONcyLEijwh8WsXpE=
github.com/G-Core/gcorelabscloud-go v0.6.0/go.mod h1:13Z1USxlxPbDFuYQyWqfNexlk4kUvOYTXbnvV/Z1lZo=
github.com/G-Core/gcorelabscdn-go v1.0.0 h1:aPjwy6UWAmi8p+Vi8OEfmDM4bOq5r+rkOi9SebbvPas=
github.com/G-Core/gcorelabscdn-go v1.0.0/go.mod h1:iSGXaTvZBzDHQW+rKFS918BgFVpONcyLEijwh8WsXpE=
github.com/G-Core/gcorelabscloud-go v0.6.2 h1:oCo3yurVnGKZLkRuQ7GVQK1LuLFEjq3mFWiX/PUI69M=
github.com/G-Core/gcorelabscloud-go v0.6.2/go.mod h1:13Z1USxlxPbDFuYQyWqfNexlk4kUvOYTXbnvV/Z1lZo=
github.com/Masterminds/goutils v1.1.1 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJLSYI=
Expand Down