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

Acl block to file #253

Merged
merged 8 commits into from
Jun 11, 2020
Merged
Show file tree
Hide file tree
Changes from 5 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
129 changes: 129 additions & 0 deletions fastly/block_fastly_service_v1_acl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package fastly

import (
"fmt"
gofastly "github.com/fastly/go-fastly/fastly"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"log"
)


var aclSchema = &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
// Required fields
"name": {
Type: schema.TypeString,
Required: true,
Description: "Unique name to refer to this ACL",
},
// Optional fields
"acl_id": {
Type: schema.TypeString,
Computed: true,
Description: "Generated acl id",
},
},
},
}


func processACL(d *schema.ResourceData, conn *gofastly.Client, latestVersion int) error {
oldACLVal, newACLVal := d.GetChange("acl")
if oldACLVal == nil {
oldACLVal = new(schema.Set)
}
if newACLVal == nil {
newACLVal = new(schema.Set)
}

oldACLSet := oldACLVal.(*schema.Set)
newACLSet := newACLVal.(*schema.Set)

remove := oldACLSet.Difference(newACLSet).List()
add := newACLSet.Difference(oldACLSet).List()

// Delete removed ACL configurations
for _, vRaw := range remove {
val := vRaw.(map[string]interface{})
opts := gofastly.DeleteACLInput{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not blocking, but in the open logging PRs, we pulled the building of Delete*Input into a separate function (see this).

Service: d.Id(),
Version: latestVersion,
Name: val["name"].(string),
}

log.Printf("[DEBUG] Fastly ACL removal opts: %#v", opts)
err := conn.DeleteACL(&opts)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not blocking, similar to the above comment, we pulled this block out into its own function (see this).


if errRes, ok := err.(*gofastly.HTTPError); ok {
if errRes.StatusCode != 404 {
return err
}
} else if err != nil {
return err
}
}

// POST new ACL configurations
for _, vRaw := range add {
val := vRaw.(map[string]interface{})
opts := gofastly.CreateACLInput{
Service: d.Id(),
Version: latestVersion,
Name: val["name"].(string),
}
Comment on lines +71 to +75
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not blocking, similar to the above comments (see this).

Pulling it out separately would make it easier to inject dependencies in tests.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, this wouldn't actually help with DI in tests. But arguably improves readability and could potentially help in future refactoring.


log.Printf("[DEBUG] Fastly ACL creation opts: %#v", opts)
_, err := conn.CreateACL(&opts)
if err != nil {
return err
}
Comment on lines +78 to +81
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not blocking, similar to the above comments (see this).

Pulling it out separately would make it easier to inject dependencies in tests.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, this wouldn't actually help with DI in tests. But arguably improves readability and could potentially help in future refactoring.

}
return nil
}


func readACL(conn *gofastly.Client, d *schema.ResourceData, s *gofastly.ServiceDetail) error {

log.Printf("[DEBUG] Refreshing ACLs for (%s)", d.Id())
aclList, err := conn.ListACLs(&gofastly.ListACLsInput{
Service: d.Id(),
Version: s.ActiveVersion.Number,
})
if err != nil {
return fmt.Errorf("[ERR] Error looking up ACLs for (%s), version (%v): %s", d.Id(), s.ActiveVersion.Number, err)
}

al := flattenACLs(aclList)

if err := d.Set("acl", al); err != nil {
log.Printf("[WARN] Error setting ACLs for (%s): %s", d.Id(), err)
}

return nil
}


func flattenACLs(aclList []*gofastly.ACL) []map[string]interface{} {
var al []map[string]interface{}
for _, acl := range aclList {
// Convert VCLs to a map for saving to state.
vclMap := map[string]interface{}{
"acl_id": acl.ID,
"name": acl.Name,
}

// prune any empty values that come from the default string value in structs
for k, v := range vclMap {
if v == "" {
delete(vclMap, k)
}
}

al = append(al, vclMap)
}

return al
}
109 changes: 5 additions & 104 deletions fastly/resource_fastly_service_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -1462,26 +1462,7 @@ func resourceServiceV1() *schema.Resource {
},
},
},
"acl": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
// Required fields
"name": {
Type: schema.TypeString,
Required: true,
Description: "Unique name to refer to this ACL",
},
// Optional fields
"acl_id": {
Type: schema.TypeString,
Computed: true,
Description: "Generated acl id",
},
},
},
},
"acl": aclSchema,
"dictionary": {
Type: schema.TypeSet,
Optional: true,
Expand Down Expand Up @@ -3103,56 +3084,8 @@ func resourceServiceV1Update(d *schema.ResourceData, meta interface{}) error {

// Find differences in ACLs
if d.HasChange("acl") {

oldACLVal, newACLVal := d.GetChange("acl")
if oldACLVal == nil {
oldACLVal = new(schema.Set)
}
if newACLVal == nil {
newACLVal = new(schema.Set)
}

oldACLSet := oldACLVal.(*schema.Set)
newACLSet := newACLVal.(*schema.Set)

remove := oldACLSet.Difference(newACLSet).List()
add := newACLSet.Difference(oldACLSet).List()

// Delete removed ACL configurations
for _, vRaw := range remove {
val := vRaw.(map[string]interface{})
opts := gofastly.DeleteACLInput{
Service: d.Id(),
Version: latestVersion,
Name: val["name"].(string),
}

log.Printf("[DEBUG] Fastly ACL removal opts: %#v", opts)
err := conn.DeleteACL(&opts)

if errRes, ok := err.(*gofastly.HTTPError); ok {
if errRes.StatusCode != 404 {
return err
}
} else if err != nil {
return err
}
}

// POST new ACL configurations
for _, vRaw := range add {
val := vRaw.(map[string]interface{})
opts := gofastly.CreateACLInput{
Service: d.Id(),
Version: latestVersion,
Name: val["name"].(string),
}

log.Printf("[DEBUG] Fastly ACL creation opts: %#v", opts)
_, err := conn.CreateACL(&opts)
if err != nil {
return err
}
if err := processACL(d, conn, latestVersion); err != nil {
return err
}
}

Expand Down Expand Up @@ -3642,19 +3575,8 @@ func resourceServiceV1Read(d *schema.ResourceData, meta interface{}) error {
}

// refresh ACLs
log.Printf("[DEBUG] Refreshing ACLs for (%s)", d.Id())
aclList, err := conn.ListACLs(&gofastly.ListACLsInput{
Service: d.Id(),
Version: s.ActiveVersion.Number,
})
if err != nil {
return fmt.Errorf("[ERR] Error looking up ACLs for (%s), version (%v): %s", d.Id(), s.ActiveVersion.Number, err)
}

al := flattenACLs(aclList)

if err := d.Set("acl", al); err != nil {
log.Printf("[WARN] Error setting ACLs for (%s): %s", d.Id(), err)
if err := readACL(conn, d, s); err != nil {
return err
}

// refresh VCL Snippets
Expand Down Expand Up @@ -4504,27 +4426,6 @@ func flattenVCLs(vclList []*gofastly.VCL) []map[string]interface{} {
return vl
}

func flattenACLs(aclList []*gofastly.ACL) []map[string]interface{} {
var al []map[string]interface{}
for _, acl := range aclList {
// Convert VCLs to a map for saving to state.
vclMap := map[string]interface{}{
"acl_id": acl.ID,
"name": acl.Name,
}

// prune any empty values that come from the default string value in structs
for k, v := range vclMap {
if v == "" {
delete(vclMap, k)
}
}

al = append(al, vclMap)
}

return al
}

func buildSnippet(snippetMap interface{}) (*gofastly.CreateSnippetInput, error) {
df := snippetMap.(map[string]interface{})
Expand Down