Skip to content
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

feat(provider): add octavia loadbalancer log subscription #637

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
114 changes: 114 additions & 0 deletions ovh/data_cloud_project_region_loadbalancer_log_subscription.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package ovh

import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"log"
"net/url"
)

func dataSourceCloudProjectRegionLoadbalancerLogSubscription() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceCloudProjectRegionLoadbalancerSubscriptionRead,
Schema: map[string]*schema.Schema{
"service_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
DefaultFunc: schema.EnvDefaultFunc("OVH_CLOUD_PROJECT_SERVICE", nil),
Description: "Service name of the resource representing the id of the cloud project.",
},
"region_name": {
Type: schema.TypeString,
Description: "Region name of the resource representing the name of the region.",
Required: true,
ForceNew: true,
},
"loadbalancer_id": {
Type: schema.TypeString,
Description: "ID representing the loadbalancer of the resource",
Required: true,
ForceNew: true,
},
"subscription_id": {
Type: schema.TypeString,
Description: "ID representing the subscription",
Required: true,
ForceNew: true,
},

//computed
"created_at": {
Type: schema.TypeString,
Description: "Creation date of the subscription",
Computed: true,
},
"kind": {
Type: schema.TypeString,
Description: "Log kind name of this subscription",
Computed: true,
},
"ldp_service_name": {
Type: schema.TypeString,
Description: "Name of the destination log service",
//Sensitive: true,
Computed: true,
},
"resource_name": {
Type: schema.TypeString,
Description: "Name of subscribed resource, where the logs come from",
Computed: true,
},
"resource_type": {
Type: schema.TypeString,
Description: "Type of subscribed resource, where the logs come from",
Computed: true,
},
"stream_id": {
Type: schema.TypeString,
Description: "Id of the target Log data platform stream",
Computed: true,
},
"updated_at": {
Type: schema.TypeString,
Description: "Last update date of the subscription",
Computed: true,
},
},
}
}

func dataSourceCloudProjectRegionLoadbalancerSubscriptionRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
regionName := d.Get("region_name").(string)
loadbalancerID := d.Get("loadbalancer_id").(string)
subscriptionID := d.Get("subscription_id").(string)

log.Printf("[DEBUG] Will read public cloud loadbalancer %s log subscription %s for region %s for project: %s", loadbalancerID, subscriptionID, regionName, serviceName)

response := &CloudProjectRegionLoadbalancerLogSubscriptionResponse{}
endpoint := fmt.Sprintf(
"/cloud/project/%s/region/%s/loadbalancing/loadbalancer/%s/log/subscription/%s",
url.PathEscape(serviceName),
url.PathEscape(regionName),
url.PathEscape(loadbalancerID),
url.PathEscape(subscriptionID),
)

if err := config.OVHClient.Get(endpoint, &response); err != nil {
return diag.Errorf("Error calling GET %s:\n\t %q", endpoint, err)
}

d.SetId(response.StreamID)
d.Set("created_at", response.CreatedAt)
d.Set("kind", response.Kind)
d.Set("resource_name", response.Resource.Name)
d.Set("resource_type", response.Resource.Type)
d.Set("ldp_service_name", response.LDPServiceName)
d.Set("stream_id", response.StreamID)
d.Set("updated_at", response.UpdatedAt)
return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package ovh
77 changes: 77 additions & 0 deletions ovh/data_cloud_project_region_loadbalancer_log_subscriptions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package ovh

import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/ovh/terraform-provider-ovh/ovh/helpers/hashcode"
"log"
"net/url"
"sort"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceCloudProjectRegionLoadbalancerLogSubscriptions() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceCloudProjectRegionLoadbalancerSubscriptionsRead,
Schema: map[string]*schema.Schema{
"service_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
DefaultFunc: schema.EnvDefaultFunc("OVH_CLOUD_PROJECT_SERVICE", nil),
Description: "Service name of the resource representing the id of the cloud project.",
},
"region_name": {
Type: schema.TypeString,
Description: "Region name of the resource representing the name of the region.",
Required: true,
ForceNew: true,
},
"loadbalancer_id": {
Type: schema.TypeString,
Description: "ID representing the loadbalancer of the resource",
Required: true,
ForceNew: true,
},
"kind": {
Type: schema.TypeString,
Optional: true,
Description: "Kind representing the loadbalancer.",
},
//computed
"subscription_ids": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}

func dataSourceCloudProjectRegionLoadbalancerSubscriptionsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
regionName := d.Get("region_name").(string)
loadbalancerID := d.Get("loadbalancer_id").(string)

log.Printf("[DEBUG] Will read public cloud loadbalancer %s log subscriptions for region %s for project: %s", loadbalancerID, regionName, serviceName)

response := make([]string, 0)
endpoint := fmt.Sprintf(
"/cloud/project/%s/region/%s/loadbalancing/loadbalancer/%s/log/subscription",
url.PathEscape(serviceName),
url.PathEscape(regionName),
url.PathEscape(loadbalancerID),
)

if err := config.OVHClient.Get(endpoint, &response); err != nil {
return diag.Errorf("Error calling GET %s:\n\t %q", endpoint, err)
}
sort.Strings(response)

d.SetId(hashcode.Strings(response))
d.Set("subscription_ids", response)
return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package ovh
3 changes: 3 additions & 0 deletions ovh/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ func Provider() *schema.Provider {
"ovh_cloud_project_kube_oidc": dataSourceCloudProjectKubeOIDC(),
"ovh_cloud_project_kube_nodepool": dataSourceCloudProjectKubeNodepool(),
"ovh_cloud_project_kube_nodes": dataSourceCloudProjectKubeNodes(),
"ovh_cloud_project_region_loadbalancer_log_subscriptions": dataSourceCloudProjectRegionLoadbalancerLogSubscriptions(),
"ovh_cloud_project_region_loadbalancer_log_subscription": dataSourceCloudProjectRegionLoadbalancerLogSubscription(),
"ovh_cloud_project_region": dataSourceCloudProjectRegion(),
"ovh_cloud_project_regions": dataSourceCloudProjectRegions(),
"ovh_cloud_project_user": datasourceCloudProjectUser(),
Expand Down Expand Up @@ -176,6 +178,7 @@ func Provider() *schema.Provider {
"ovh_cloud_project_network_private": resourceCloudProjectNetworkPrivate(),
"ovh_cloud_project_network_private_subnet": resourceCloudProjectNetworkPrivateSubnet(),
"ovh_cloud_project_region_storage_presign": resourceCloudProjectRegionStoragePresign(),
"ovh_cloud_project_region_loadbalancer_log_subscription": resourceCloudProjectRegionLoadbalancerLogSubscription(),
"ovh_cloud_project_user": resourceCloudProjectUser(),
"ovh_cloud_project_user_s3_credential": resourceCloudProjectUserS3Credential(),
"ovh_cloud_project_user_s3_policy": resourceCloudProjectUserS3Policy(),
Expand Down