-
Notifications
You must be signed in to change notification settings - Fork 142
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
Acl block to file #253
Changes from all commits
2875b0a
78cf81e
67743c7
3d5c355
df2dc2e
199cf84
5c4b4da
e67c732
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} |
There was a problem hiding this comment.
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).