-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
vladhanzha
committed
Oct 14, 2024
1 parent
3bc4984
commit 74b7b2b
Showing
6 changed files
with
367 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package cloudconnexa | ||
|
||
import ( | ||
"context" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/openvpn/cloudconnexa-go-client/v2/cloudconnexa" | ||
) | ||
|
||
func dataSourceLocationContext() *schema.Resource { | ||
return &schema.Resource{ | ||
ReadContext: dataSourceLocationContextRead, | ||
Schema: map[string]*schema.Schema{ | ||
"id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"description": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"user_groups_ids": { | ||
Type: schema.TypeList, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
Computed: true, | ||
}, | ||
"ip_policy": { | ||
Type: schema.TypeList, | ||
Elem: ipPolicyConfig(), | ||
Computed: true, | ||
}, | ||
"country_policy": { | ||
Type: schema.TypeList, | ||
Elem: countryPolicyConfig(), | ||
Computed: true, | ||
}, | ||
"default_policy": { | ||
Type: schema.TypeList, | ||
Elem: defaultPolicyConfig(), | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceLocationContextRead(ctx context.Context, data *schema.ResourceData, i interface{}) diag.Diagnostics { | ||
c := i.(*cloudconnexa.Client) | ||
var diags diag.Diagnostics | ||
var id = data.Get("id").(string) | ||
context, err := c.LocationContexts.Get(id) | ||
|
||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
if context == nil { | ||
return append(diags, diag.Errorf("Location Context with ID %s was not found", id)...) | ||
} | ||
setLocationContextData(data, context) | ||
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
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,252 @@ | ||
package cloudconnexa | ||
|
||
import ( | ||
"context" | ||
"github.com/openvpn/cloudconnexa-go-client/v2/cloudconnexa" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" | ||
) | ||
|
||
func resourceLocationContext() *schema.Resource { | ||
return &schema.Resource{ | ||
Description: "Use `cloudconnexa_location_context` to create a Location Context policy.", | ||
CreateContext: resourceLocationContextCreate, | ||
ReadContext: resourceLocationContextRead, | ||
DeleteContext: resourceLocationContextDelete, | ||
UpdateContext: resourceLocationContextUpdate, | ||
Importer: &schema.ResourceImporter{ | ||
StateContext: schema.ImportStatePassthroughContext, | ||
}, | ||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: "The Location Context name.", | ||
}, | ||
"description": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Default: "Managed by Terraform", | ||
ValidateFunc: validation.StringLenBetween(1, 120), | ||
Description: "The description for the UI. Defaults to `Managed by Terraform`.", | ||
}, | ||
"user_groups_ids": { | ||
Type: schema.TypeList, | ||
Required: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
Description: "List of User Group IDs assigned to this policy.", | ||
}, | ||
"ip_policy": { | ||
Type: schema.TypeList, | ||
MaxItems: 1, | ||
Optional: true, | ||
AtLeastOneOf: []string{"ip_policy", "country_policy"}, | ||
Elem: ipPolicyConfig(), | ||
}, | ||
"country_policy": { | ||
Type: schema.TypeList, | ||
MaxItems: 1, | ||
Optional: true, | ||
Elem: countryPolicyConfig(), | ||
}, | ||
"default_policy": { | ||
Type: schema.TypeList, | ||
MaxItems: 1, | ||
Required: true, | ||
Elem: defaultPolicyConfig(), | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func ipPolicyConfig() *schema.Resource { | ||
return &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"allowed": { | ||
Type: schema.TypeBool, | ||
Required: true, | ||
}, | ||
"ips": { | ||
Type: schema.TypeList, | ||
Required: true, | ||
Elem: ipConfig(), | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func ipConfig() *schema.Resource { | ||
return &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"ip": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"description": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func countryPolicyConfig() *schema.Resource { | ||
return &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"allowed": { | ||
Type: schema.TypeBool, | ||
Required: true, | ||
}, | ||
"countries": { | ||
Type: schema.TypeList, | ||
Required: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func defaultPolicyConfig() *schema.Resource { | ||
return &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"allowed": { | ||
Type: schema.TypeBool, | ||
Required: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceLocationContextCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
c := m.(*cloudconnexa.Client) | ||
var diags diag.Diagnostics | ||
dr := resourceDataToLocationContext(d) | ||
response, err := c.LocationContexts.Create(dr) | ||
if err != nil { | ||
return append(diags, diag.FromErr(err)...) | ||
} | ||
d.SetId(response.Id) | ||
return diags | ||
} | ||
|
||
func resourceLocationContextRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
c := m.(*cloudconnexa.Client) | ||
var diags diag.Diagnostics | ||
recordId := d.Id() | ||
lc, err := c.LocationContexts.Get(recordId) | ||
if err != nil { | ||
return append(diags, diag.FromErr(err)...) | ||
} | ||
if lc == nil { | ||
d.SetId("") | ||
} else { | ||
setLocationContextData(d, lc) | ||
} | ||
return diags | ||
} | ||
|
||
func resourceLocationContextUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
c := m.(*cloudconnexa.Client) | ||
var diags diag.Diagnostics | ||
lc := resourceDataToLocationContext(d) | ||
_, err := c.LocationContexts.Update(d.Id(), lc) | ||
if err != nil { | ||
return append(diags, diag.FromErr(err)...) | ||
} | ||
return diags | ||
} | ||
|
||
func resourceLocationContextDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
c := m.(*cloudconnexa.Client) | ||
var diags diag.Diagnostics | ||
routeId := d.Id() | ||
err := c.LocationContexts.Delete(routeId) | ||
if err != nil { | ||
return append(diags, diag.FromErr(err)...) | ||
} | ||
return diags | ||
} | ||
|
||
func setLocationContextData(d *schema.ResourceData, lc *cloudconnexa.LocationContext) { | ||
d.SetId(lc.Id) | ||
d.Set("name", lc.Name) | ||
d.Set("description", lc.Description) | ||
d.Set("user_groups_ids", lc.UserGroupsIds) | ||
|
||
if lc.IpPolicy != nil { | ||
ipPolicy := make(map[string]interface{}) | ||
ipPolicy["allowed"] = lc.IpPolicy.Allowed | ||
var ips []interface{} | ||
for _, ip := range lc.IpPolicy.Ips { | ||
ips = append(ips, map[string]interface{}{ | ||
"ip": ip.Ip, | ||
"description": ip.Description, | ||
}) | ||
} | ||
ipPolicy["ips"] = ips | ||
d.Set("ip_policy", []interface{}{ipPolicy}) | ||
} | ||
|
||
if lc.CountryPolicy != nil { | ||
countryPolicy := make(map[string]interface{}) | ||
countryPolicy["allowed"] = lc.CountryPolicy.Allowed | ||
countryPolicy["countries"] = lc.CountryPolicy.Countries | ||
d.Set("country_policy", []interface{}{countryPolicy}) | ||
} | ||
|
||
defaultPolicy := make(map[string]interface{}) | ||
defaultPolicy["allowed"] = lc.DefaultPolicy.Allowed | ||
d.Set("default_policy", []interface{}{defaultPolicy}) | ||
} | ||
|
||
func resourceDataToLocationContext(data *schema.ResourceData) *cloudconnexa.LocationContext { | ||
defaultPolicyData := data.Get("default_policy").([]interface{})[0].(map[string]interface{}) | ||
defaultPolicy := &cloudconnexa.DefaultPolicy{ | ||
Allowed: defaultPolicyData["allowed"].(bool), | ||
} | ||
|
||
response := &cloudconnexa.LocationContext{ | ||
Id: data.Id(), | ||
Name: data.Get("name").(string), | ||
Description: data.Get("description").(string), | ||
DefaultPolicy: defaultPolicy, | ||
} | ||
|
||
for _, id := range data.Get("user_groups_ids").([]interface{}) { | ||
response.UserGroupsIds = append(response.UserGroupsIds, id.(string)) | ||
} | ||
|
||
ipPolicyList := data.Get("ip_policy").([]interface{}) | ||
if len(ipPolicyList) > 0 { | ||
ipPolicy := &cloudconnexa.IpPolicy{} | ||
ipPolicyData := ipPolicyList[0].(map[string]interface{}) | ||
ipPolicy.Allowed = ipPolicyData["allowed"].(bool) | ||
for _, ip := range ipPolicyData["ips"].([]interface{}) { | ||
ipPolicy.Ips = append(ipPolicy.Ips, cloudconnexa.Ip{ | ||
Ip: ip.(map[string]interface{})["ip"].(string), | ||
Description: ip.(map[string]interface{})["description"].(string), | ||
}) | ||
} | ||
response.IpPolicy = ipPolicy | ||
} | ||
|
||
countryPolicyList := data.Get("country_policy").([]interface{}) | ||
if len(countryPolicyList) > 0 && countryPolicyList[0] != nil { | ||
countryPolicyData := data.Get("country_policy").([]interface{})[0].(map[string]interface{}) | ||
countryPolicy := &cloudconnexa.CountryPolicy{ | ||
Allowed: countryPolicyData["allowed"].(bool), | ||
} | ||
for _, country := range countryPolicyData["countries"].([]interface{}) { | ||
countryPolicy.Countries = append(countryPolicy.Countries, country.(string)) | ||
} | ||
response.CountryPolicy = countryPolicy | ||
} | ||
|
||
return response | ||
} |
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,23 @@ | ||
resource "cloudconnexa_location_context" "this" { | ||
name = "Location Context Policy" | ||
description = "Description for Location Context Policy" | ||
user_groups_ids = [] | ||
ip_policy { | ||
allowed = true | ||
ips { | ||
ip = "10.10.0.0/16" | ||
description = "Test subnet" | ||
} | ||
ips { | ||
ip = "10.20.0.0/16" | ||
description = "Test subnet 2" | ||
} | ||
} | ||
country_policy { | ||
allowed = true | ||
countries = ["US", "GB"] | ||
} | ||
default_policy { | ||
allowed = false | ||
} | ||
} |
Oops, something went wrong.