Skip to content

Commit

Permalink
Merge pull request #253 from opencredo/oc/acl_block_to_file
Browse files Browse the repository at this point in the history
Acl block to file
  • Loading branch information
phamann authored Jun 11, 2020
2 parents 463be28 + e67c732 commit 54b55f5
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 105 deletions.
126 changes: 126 additions & 0 deletions fastly/block_fastly_service_v1_acl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package fastly

import (
"fmt"
"log"

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

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{
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
}
}
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
}
File renamed without changes.
110 changes: 5 additions & 105 deletions fastly/resource_fastly_service_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -1465,26 +1465,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 @@ -3122,56 +3103,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 @@ -3661,19 +3594,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 @@ -4523,28 +4445,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{})
opts := gofastly.CreateSnippetInput{
Expand Down

0 comments on commit 54b55f5

Please sign in to comment.