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

add authorization for OriginGroup #185

Closed
wants to merge 4 commits into from
Closed
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
21 changes: 21 additions & 0 deletions docs/resources/cdn_origingroup.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ resource "edgecenter_cdn_origingroup" "origin_group_1" {
enabled = true
backup = true
}
authorization {
access_key_id = "test_access_key_id"
auth_type = "aws_signature_v2"
bucket_name = "test_bucket_name"
secret_key = "keywqueiuqwiueiqweqwiueiqwiueuiqw"
}
consistent_balancing = true
}
```
Expand All @@ -43,6 +49,10 @@ resource "edgecenter_cdn_origingroup" "origin_group_1" {
- `origin` (Block Set, Min: 1) Add information about your sources. (see [below for nested schema](#nestedblock--origin))
- `use_next` (Boolean) Specify whether or not the CDN will use the next source in the list if your source responds with an HTTP status code of 4XX or 5XX.

### Optional

- `authorization` (Block Set, Max: 1) Add information about authorization. (see [below for nested schema](#nestedblock--authorization))

### Read-Only

- `id` (String) The ID of this resource.
Expand All @@ -62,3 +72,14 @@ Optional:
Read-Only:

- `id` (Number)


<a id="nestedblock--authorization"></a>
### Nested Schema for `authorization`

Required:

- `access_key_id` (String) Specify the access key ID in 20 alphanumeric characters.
- `auth_type` (String) The type of authorization on the source. It can take two values - aws_signature_v2 or aws_signature_v4.
- `bucket_name` (String) Specify the bucket name. The name is restricted to 255 symbols and may include alphanumeric characters, slashes, pluses, hyphens, and underscores.
- `secret_key` (String) Specify the secret access key. The value must be between 32 and 40 characters and may include alphanumeric characters, slashes, pluses, hyphens, and underscores.
6 changes: 6 additions & 0 deletions docs/resources/cdn_resource.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ resource "edgecenter_cdn_origingroup" "origin_group_1" {
enabled = true
backup = true
}
authorization {
access_key_id = "test-access_key_id"
auth_type = "aws_signature_v2"
bucket_name = "test-bucket_name"
secret_key = "key"
}
consistent_balancing = true
}

Expand Down
81 changes: 81 additions & 0 deletions edgecenter/resource_edgecenter_cdn_origin_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,36 @@ func resourceCDNOriginGroup() *schema.Resource {
},
},
},
"authorization": {
Type: schema.TypeSet,
MaxItems: 1,
Optional: true,
Description: "Add information about authorization.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"auth_type": {
Type: schema.TypeString,
Required: true,
Description: "The type of authorization on the source. It can take two values - aws_signature_v2 or aws_signature_v4.",
},
"access_key_id": {
Type: schema.TypeString,
Required: true,
Description: "Specify the access key ID in 20 alphanumeric characters.",
},
"secret_key": {
Type: schema.TypeString,
Required: true,
Description: "Specify the secret access key. The value must be between 32 and 40 characters and may include alphanumeric characters, slashes, pluses, hyphens, and underscores.",
},
"bucket_name": {
Type: schema.TypeString,
Required: true,
Description: "Specify the bucket name. The name is restricted to 255 symbols and may include alphanumeric characters, slashes, pluses, hyphens, and underscores.",
},
},
},
},
"consistent_balancing": {
Type: schema.TypeBool,
Required: true,
Expand All @@ -84,6 +114,7 @@ func resourceCDNOriginGroupCreate(ctx context.Context, d *schema.ResourceData, m
req.Name = d.Get("name").(string)
req.UseNext = d.Get("use_next").(bool)
req.Origins = setToOriginRequests(d.Get("origin").(*schema.Set))
req.Authorization = setToAuthRequest(d.Get("authorization").(*schema.Set))
req.ConsistentBalancing = d.Get("consistent_balancing").(bool)

result, err := client.OriginGroups().Create(ctx, &req)
Expand Down Expand Up @@ -120,6 +151,10 @@ func resourceCDNOriginGroupRead(ctx context.Context, d *schema.ResourceData, m i
if err := d.Set("origin", originsToSet(result.Origins)); err != nil {
return diag.FromErr(err)
}
if err := d.Set("authorization", authToSet(result.Authorization)); err != nil {
return diag.FromErr(err)
}
d.Set("consistent_balancing", result.ConsistentBalancing)

log.Println("[DEBUG] Finish CDN OriginGroup reading")

Expand All @@ -141,6 +176,8 @@ func resourceCDNOriginGroupUpdate(ctx context.Context, d *schema.ResourceData, m
req.Name = d.Get("name").(string)
req.UseNext = d.Get("use_next").(bool)
req.Origins = setToOriginRequests(d.Get("origin").(*schema.Set))
req.Authorization = setToAuthRequest(d.Get("authorization").(*schema.Set))
req.ConsistentBalancing = d.Get("consistent_balancing").(bool)

if _, err := client.OriginGroups().Update(ctx, id, &req); err != nil {
return diag.FromErr(err)
Expand Down Expand Up @@ -222,3 +259,47 @@ func originSetIDFunc(i interface{}) int {

return int(binary.BigEndian.Uint64(h.Sum(nil)))
}

func setToAuthRequest(set *schema.Set) *origingroups.Authorization {
if set.Len() == 0 {
return nil
}

fields := set.List()[0].(map[string]interface{})
return &origingroups.Authorization{
AuthType: fields["auth_type"].(string),
AccessKeyID: fields["access_key_id"].(string),
SecretKey: fields["secret_key"].(string),
BucketName: fields["bucket_name"].(string),
}
}

func authToSet(auth *origingroups.Authorization) *schema.Set {
if auth == nil {
return nil
}

return schema.NewSet(schema.HashResource(&schema.Resource{
Schema: map[string]*schema.Schema{
"auth_type": {
Type: schema.TypeString,
},
"access_key_id": {
Type: schema.TypeString,
},
"secret_key": {
Type: schema.TypeString,
},
"bucket_name": {
Type: schema.TypeString,
},
},
}), []interface{}{
map[string]interface{}{
"auth_type": auth.AuthType,
"access_key_id": auth.AccessKeyID,
"secret_key": auth.SecretKey,
"bucket_name": auth.BucketName,
},
})
}
7 changes: 7 additions & 0 deletions edgecenter/test/resource_edgecenter_cdn_origin_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ func TestAccOriginGroup(t *testing.T) {
source = "yandex.ru"
enabled = true
}

authorization {
access_key_id = "test_access_key_id"
auth_type = "aws_signature_v2"
bucket_name = "test_bucket_name"
secret_key = "keywqueiuqwiueiqweqwiueiqwiueuiqw"
}
consistent_balancing = true
}
`, params.Source, params.Enabled)
Expand Down
6 changes: 6 additions & 0 deletions examples/resources/edgecenter_cdn_origingroup/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,11 @@ resource "edgecenter_cdn_origingroup" "origin_group_1" {
enabled = true
backup = true
}
authorization {
access_key_id = "test_access_key_id"
auth_type = "aws_signature_v2"
bucket_name = "test_bucket_name"
secret_key = "keywqueiuqwiueiqweqwiueiqwiueuiqw"
}
consistent_balancing = true
}
6 changes: 6 additions & 0 deletions examples/resources/edgecenter_cdn_resource/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ resource "edgecenter_cdn_origingroup" "origin_group_1" {
enabled = true
backup = true
}
authorization {
access_key_id = "test-access_key_id"
auth_type = "aws_signature_v2"
bucket_name = "test-bucket_name"
secret_key = "key"
}
consistent_balancing = true
}

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/Edge-Center/edgecenter-dns-sdk-go v0.1.3
github.com/Edge-Center/edgecenter-storage-sdk-go v0.2.0
github.com/Edge-Center/edgecentercdn-go v0.1.10
github.com/Edge-Center/edgecentercdn-go v0.1.11
github.com/Edge-Center/edgecentercloud-go v0.1.11
github.com/Edge-Center/edgecentercloud-go/v2 v2.1.4-0.20240703075841-dfdec037dd37
github.com/connerdouglass/go-retry v1.0.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ github.com/Edge-Center/edgecenter-storage-sdk-go v0.2.0 h1:1aPDpywWbaF7VEjP/GjVo
github.com/Edge-Center/edgecenter-storage-sdk-go v0.2.0/go.mod h1:TcWO0BPvDsE6AGlPBqpKCZhoQ70rRlqmm85J32qcL8I=
github.com/Edge-Center/edgecentercdn-go v0.1.10 h1:Hj+SjvPClhOYtpmoS2g33WrxyHitUBHroNSwRBZ9P1U=
github.com/Edge-Center/edgecentercdn-go v0.1.10/go.mod h1:RwEyxwPAmxor1mZKUTa2bIU2p5qM6kcAofUkaE4O1V4=
github.com/Edge-Center/edgecentercdn-go v0.1.11 h1:jTLxmLLmHBiIcvUsBTS5DtXB0SLFLpqKoe8ts2l9WCc=
github.com/Edge-Center/edgecentercdn-go v0.1.11/go.mod h1:RwEyxwPAmxor1mZKUTa2bIU2p5qM6kcAofUkaE4O1V4=
github.com/Edge-Center/edgecentercloud-go v0.1.11 h1:00h5o/71lEoSdU1B4AWmviuOfO28P6nsRP+afjIsW80=
github.com/Edge-Center/edgecentercloud-go v0.1.11/go.mod h1:kmXGtx0lL1ib+SPfJe/uIAyDHamquAvqiftoLSyhxF8=
github.com/Edge-Center/edgecentercloud-go/v2 v2.1.4-0.20240703075841-dfdec037dd37 h1:Q2qKSUaKOxXr9s5ACVC5/zcQMjjFBTX/NUwoTQS/4m8=
Expand Down
Loading