-
Notifications
You must be signed in to change notification settings - Fork 626
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GATE-2448: Adds support for proxy endpoints
- Loading branch information
Michael Borkenstein
committed
Mar 17, 2022
1 parent
ed246a3
commit 70f731a
Showing
5 changed files
with
234 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
cloudflare/resource_cloudflare_teams_proxy_endpoints.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package cloudflare | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"strings" | ||
|
||
"github.com/cloudflare/cloudflare-go" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func resourceCloudflareTeamsProxyEndpoint() *schema.Resource { | ||
return &schema.Resource{ | ||
Schema: resourceCloudflareTeamsProxyEndpointSchema(), | ||
Create: resourceCloudflareTeamsProxyEndpointCreate, | ||
Read: resourceCloudflareTeamsProxyEndpointRead, | ||
Update: resourceCloudflareTeamsProxyEndpointUpdate, | ||
Delete: resourceCloudflareTeamsProxyEndpointDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: resourceCloudflareTeamsProxyEndpointImport, | ||
}, | ||
} | ||
} | ||
|
||
func resourceCloudflareTeamsProxyEndpointRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*cloudflare.API) | ||
accountID := d.Get("account_id").(string) | ||
|
||
endpoint, err := client.TeamsProxyEndpoint(context.Background(), accountID, d.Id()) | ||
if err != nil { | ||
if strings.Contains(err.Error(), "HTTP status 400") { | ||
log.Printf("[INFO] Teams Proxy Endpoint %s no longer exists", d.Id()) | ||
d.SetId("") | ||
return nil | ||
} | ||
return fmt.Errorf("error finding Teams Proxy Endpoint %q: %s", d.Id(), err) | ||
} | ||
|
||
if err := d.Set("name", endpoint.Name); err != nil { | ||
return fmt.Errorf("error parsing Proxy Endpoint name") | ||
} | ||
if err := d.Set("ips", endpoint.IPs); err != nil { | ||
return fmt.Errorf("error parsing Proxy Endpoint IPs") | ||
} | ||
if err := d.Set("subdomain", endpoint.Subdomain); err != nil { | ||
return fmt.Errorf("error parsing Proxy Endpoint subdomain") | ||
} | ||
return nil | ||
} | ||
func resourceCloudflareTeamsProxyEndpointCreate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*cloudflare.API) | ||
|
||
accountID := d.Get("account_id").(string) | ||
newProxyEndpoint := cloudflare.TeamsProxyEndpoint{ | ||
Name: d.Get("name").(string), | ||
IPs: inflateProxyEndpointIPs(d.Get("ips").([]interface{})), | ||
} | ||
|
||
log.Printf("[DEBUG] Creating Cloudflare Teams Proxy Endpoint from struct: %+v", newProxyEndpoint) | ||
|
||
proxyEndpoint, err := client.CreateTeamsProxyEndpoint(context.Background(), accountID, newProxyEndpoint) | ||
if err != nil { | ||
return fmt.Errorf("error creating Teams Proxy Endpoint for account %q: %s", accountID, err) | ||
} | ||
|
||
d.SetId(proxyEndpoint.ID) | ||
return resourceCloudflareTeamsProxyEndpointRead(d, meta) | ||
|
||
} | ||
func resourceCloudflareTeamsProxyEndpointUpdate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*cloudflare.API) | ||
accountID := d.Get("account_id").(string) | ||
updatedProxyEndpoint := cloudflare.TeamsProxyEndpoint{ | ||
ID: d.Id(), | ||
Name: d.Get("name").(string), | ||
IPs: inflateProxyEndpointIPs(d.Get("ips").([]interface{})), | ||
} | ||
log.Printf("[DEBUG] Updating Cloudflare Teams Proxy Endpoint from struct: %+v", updatedProxyEndpoint) | ||
|
||
teamsProxyEndpoint, err := client.UpdateTeamsProxyEndpoint(context.Background(), accountID, updatedProxyEndpoint) | ||
if err != nil { | ||
return fmt.Errorf("error updating Teams Proxy Endpoint for account %q: %s", accountID, err) | ||
} | ||
if teamsProxyEndpoint.ID == "" { | ||
return fmt.Errorf("failed to find Teams Proxy Endpoint ID in update response; resource was empty") | ||
} | ||
return resourceCloudflareTeamsProxyEndpointRead(d, meta) | ||
} | ||
|
||
func resourceCloudflareTeamsProxyEndpointDelete(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*cloudflare.API) | ||
id := d.Id() | ||
accountID := d.Get("account_id").(string) | ||
|
||
log.Printf("[DEBUG] Deleting Cloudflare Teams Proxy Endpoint using ID: %s", id) | ||
|
||
err := client.DeleteTeamsProxyEndpoint(context.Background(), accountID, id) | ||
if err != nil { | ||
return fmt.Errorf("error deleting Teams Proxy Endpoint for account %q: %s", accountID, err) | ||
} | ||
|
||
return resourceCloudflareTeamsProxyEndpointRead(d, meta) | ||
} | ||
|
||
func resourceCloudflareTeamsProxyEndpointImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { | ||
attributes := strings.SplitN(d.Id(), "/", 2) | ||
|
||
if len(attributes) != 2 { | ||
return nil, fmt.Errorf("invalid id (\"%s\") specified, should be in format \"accountID/teamsProxyEndpointID\"", d.Id()) | ||
} | ||
|
||
accountID, teamsProxyEndpointID := attributes[0], attributes[1] | ||
|
||
log.Printf("[DEBUG] Importing Cloudflare Teams Proxy Endpoint: id %s for account %s", teamsProxyEndpointID, accountID) | ||
|
||
d.Set("account_id", accountID) | ||
d.SetId(teamsProxyEndpointID) | ||
|
||
err := resourceCloudflareTeamsProxyEndpointRead(d, meta) | ||
|
||
return []*schema.ResourceData{d}, err | ||
|
||
} | ||
|
||
func inflateProxyEndpointIPs(ips []interface{}) []string { | ||
i := make([]string, len(ips)) | ||
for x, ip := range ips { | ||
i[x] = ip.(string) | ||
} | ||
return i | ||
} |
74 changes: 74 additions & 0 deletions
74
cloudflare/resource_cloudflare_teams_proxy_endpoints_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package cloudflare | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/cloudflare/cloudflare-go" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform" | ||
) | ||
|
||
func TestAccCloudflareTeamsProxyEndpointBasic(t *testing.T) { | ||
// Temporarily unset CLOUDFLARE_API_TOKEN if it is set as the Access | ||
// service does not yet support the API tokens and it results in | ||
// misleading state error messages. | ||
if os.Getenv("CLOUDFLARE_API_TOKEN") != "" { | ||
defer func(apiToken string) { | ||
os.Setenv("CLOUDFLARE_API_TOKEN", apiToken) | ||
}(os.Getenv("CLOUDFLARE_API_TOKEN")) | ||
os.Setenv("CLOUDFLARE_API_TOKEN", "") | ||
} | ||
|
||
rnd := generateRandomResourceName() | ||
name := fmt.Sprintf("cloudflare_teams_proxy_endpoint.%s", rnd) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { | ||
testAccessAccPreCheck(t) | ||
}, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckCloudflareTeamsProxyEndpointDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccCloudflareTeamsProxyEndpointConfigBasic(rnd, accountID), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr(name, "account_id", accountID), | ||
resource.TestCheckResourceAttr(name, "name", rnd), | ||
resource.TestCheckResourceAttr(name, "ips.0", "1.2.3.4/32"), | ||
resource.TestMatchResourceAttr(name, "subdomain", regexp.MustCompile("^[a-zA-Z0-9]+$")), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCloudflareTeamsProxyEndpointConfigBasic(rnd, accountID string) string { | ||
return fmt.Sprintf(` | ||
resource "cloudflare_teams_proxy_endpoint" "%[1]s" { | ||
name = "%[1]s" | ||
account_id = "%[2]s" | ||
ips = ["1.2.3.4/32"] | ||
} | ||
`, rnd, accountID) | ||
} | ||
|
||
func testAccCheckCloudflareTeamsProxyEndpointDestroy(s *terraform.State) error { | ||
client := testAccProvider.Meta().(*cloudflare.API) | ||
|
||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "cloudflare_teams_proxy_endpoint" { | ||
continue | ||
} | ||
|
||
_, err := client.TeamsProxyEndpoint(context.Background(), rs.Primary.Attributes["account_id"], rs.Primary.ID) | ||
if err == nil { | ||
return fmt.Errorf("teams Proxy Endpoint still exists") | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package cloudflare | ||
|
||
import "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
|
||
func resourceCloudflareTeamsProxyEndpointSchema() map[string]*schema.Schema { | ||
|
||
return map[string]*schema.Schema{ | ||
"account_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"subdomain": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"ips": { | ||
Type: schema.TypeList, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
Required: true, | ||
}, | ||
} | ||
} |