Skip to content

Commit

Permalink
(CLOUDDEV-354): listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
anaxaim committed Dec 12, 2023
1 parent fab3b1a commit f6656b1
Show file tree
Hide file tree
Showing 9 changed files with 524 additions and 7 deletions.
142 changes: 142 additions & 0 deletions edgecenter/lblistener/datasource_lblistener.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package lblistener

import (
"context"

"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"

edgecloud "github.com/Edge-Center/edgecentercloud-go"
"github.com/Edge-Center/edgecentercloud-go/util"
"github.com/Edge-Center/terraform-provider-edgecenter/edgecenter/config"
)

func DataSourceEdgeCenterLbListener() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceEdgeCenterLbListenerRead,
Description: `A listener is a process that checks for connection requests using the protocol and port that you configure.`,

Schema: map[string]*schema.Schema{
"project_id": {
Type: schema.TypeInt,
Required: true,
Description: "uuid of the project",
},
"region_id": {
Type: schema.TypeInt,
Required: true,
Description: "uuid of the region",
},
"id": {
Type: schema.TypeString,
Optional: true,
Description: "listener uuid",
ValidateFunc: validation.IsUUID,
ExactlyOneOf: []string{"id", "name"},
},
"name": {
Type: schema.TypeString,
Optional: true,
Description: `listener name. this parameter is not unique, if there is more than one listener with the same name,
then the first one will be used. it is recommended to use "id"`,
ExactlyOneOf: []string{"id", "name"},
},
"loadbalancer_id": {
Type: schema.TypeString,
Required: true,
Description: "ID of the load balancer",
},
// computed attributes
"protocol": {
Type: schema.TypeString,
Computed: true,
Description: "protocol of the load balancer",
},
"protocol_port": {
Type: schema.TypeInt,
Computed: true,
Description: "protocol port number of the resource",
},
"secret_id": {
Type: schema.TypeString,
Computed: true,
Description: "ID of the secret where PKCS12 file is stored for the TERMINATED_HTTPS load balancer",
},
"provisioning_status": {
Type: schema.TypeString,
Computed: true,
Description: "lifecycle status of the listener",
},
"operating_status": {
Type: schema.TypeString,
Computed: true,
Description: "operating status of the listener",
},
"pool_count": {
Type: schema.TypeInt,
Computed: true,
Description: "number of pools",
},
"insert_headers": {
Type: schema.TypeMap,
Computed: true,
Description: "dictionary of additional header insertion into the HTTP headers. only used with the HTTP and TERMINATED_HTTPS protocols",
},
"allowed_cidrs": {
Type: schema.TypeList,
Computed: true,
Description: "allowed CIDRs for listener.",
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}

func dataSourceEdgeCenterLbListenerRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*config.CombinedConfig).EdgeCloudClient()
client.Region = d.Get("region_id").(int)
client.Project = d.Get("project_id").(int)

loadbalancerID := d.Get("loadbalancer_id").(string)

var foundListener *edgecloud.Listener

if id, ok := d.GetOk("id"); ok {
listener, _, err := client.Loadbalancers.ListenerGet(ctx, id.(string))
if err != nil {
return diag.FromErr(err)
}

foundListener = listener
} else if listenerName, ok := d.GetOk("name"); ok {
listener, err := util.LBListenerGetByName(ctx, client, listenerName.(string), loadbalancerID)
if err != nil {
return diag.FromErr(err)
}

foundListener = listener
} else {
return diag.Errorf("Error: specify either id or a name to lookup the listener")
}

d.SetId(foundListener.ID)
d.Set("name", foundListener.Name)
d.Set("loadbalancer_id", loadbalancerID)
d.Set("provisioning_status", foundListener.ProvisioningStatus)
d.Set("operating_status", foundListener.OperatingStatus)
d.Set("protocol", foundListener.Protocol)
d.Set("protocol_port", foundListener.ProtocolPort)
d.Set("pool_count", foundListener.PoolCount)
d.Set("secret_id", foundListener.SecretID)

if err := setAllowedCIDRs(ctx, d, foundListener); err != nil {
return diag.FromErr(err)
}

if err := setInsertHeaders(ctx, d, foundListener); err != nil {
return diag.FromErr(err)
}

return nil
}
108 changes: 108 additions & 0 deletions edgecenter/lblistener/lblistener.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package lblistener

import (
"github.com/hashicorp/go-cty/cty"
"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"

edgecloud "github.com/Edge-Center/edgecentercloud-go"
)

func lblistenerSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"project_id": {
Type: schema.TypeInt,
Required: true,
Description: "uuid of the project",
},
"region_id": {
Type: schema.TypeInt,
Required: true,
Description: "uuid of the region",
},
"name": {
Type: schema.TypeString,
Required: true,
Description: `listener name`,
},
"loadbalancer_id": {
Type: schema.TypeString,
Required: true,
Description: "ID of the load balancer",
ValidateFunc: validation.IsUUID,
},
"protocol": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Available values are 'HTTP', 'HTTPS', 'TCP', 'UDP' and 'Terminated HTTPS'",
ValidateDiagFunc: func(val interface{}, key cty.Path) diag.Diagnostics {
v := val.(string)
switch edgecloud.LoadbalancerListenerProtocol(v) {
case edgecloud.ListenerProtocolHTTP, edgecloud.ListenerProtocolHTTPS, edgecloud.ListenerProtocolTCP,
edgecloud.ListenerProtocolUDP, edgecloud.ListenerProtocolTerminatedHTTPS:
return diag.Diagnostics{}
default:
return diag.Errorf("wrong protocol %s, available values are 'HTTP', 'HTTPS', 'TCP', 'UDP' and 'Terminated HTTPS'", v)
}
},
},
"protocol_port": {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
Description: "port on which the protocol is bound",
},
"insert_x_forwarded": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Description: "add headers X-Forwarded-For, X-Forwarded-Port, X-Forwarded-Proto to requests. only used with HTTP or TERMINATED_HTTPS protocols",
},
"secret_id": {
Type: schema.TypeString,
Optional: true,
Description: "ID of the secret where PKCS12 file is stored for the TERMINATED_HTTPS load balancer",
ValidateFunc: validation.IsUUID,
},
"sni_secret_id": {
Type: schema.TypeList,
Optional: true,
Description: "list of secret identifiers used for Server Name Indication (SNI).",
Elem: &schema.Schema{Type: schema.TypeString},
},
"allowed_cidrs": {
Type: schema.TypeList,
Optional: true,
Description: "the allowed CIDRs for listener",
Elem: &schema.Schema{Type: schema.TypeString},
},
// computed attributes
"id": {
Type: schema.TypeString,
Computed: true,
Description: "listener uuid",
},
"operating_status": {
Type: schema.TypeString,
Computed: true,
Description: "operating status of the listener",
},
"provisioning_status": {
Type: schema.TypeString,
Computed: true,
Description: "lifecycle status of the listener",
},
"pool_count": {
Type: schema.TypeInt,
Computed: true,
Description: "number of pools",
},
"insert_headers": {
Type: schema.TypeMap,
Computed: true,
Description: "dictionary of additional header insertion into the HTTP headers. only used with the HTTP and TERMINATED_HTTPS protocols",
},
}
}
Loading

0 comments on commit f6656b1

Please sign in to comment.