|
| 1 | +package ovh |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "net/url" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 9 | +) |
| 10 | + |
| 11 | +func dataSourceDbaasLogs() *schema.Resource { |
| 12 | + return &schema.Resource{ |
| 13 | + Read: func(d *schema.ResourceData, meta interface{}) error { |
| 14 | + return dataSourceDbaasLogsRead(d, meta) |
| 15 | + }, |
| 16 | + Schema: map[string]*schema.Schema{ |
| 17 | + "service_name": { |
| 18 | + Type: schema.TypeString, |
| 19 | + Description: "The service name", |
| 20 | + Required: true, |
| 21 | + }, |
| 22 | + "cluster_type": { |
| 23 | + Type: schema.TypeString, |
| 24 | + Description: "Cluster type", |
| 25 | + Computed: true, |
| 26 | + }, |
| 27 | + "dedicated_input_pem": { |
| 28 | + Type: schema.TypeString, |
| 29 | + Description: "PEM for dedicated inputs", |
| 30 | + Computed: true, |
| 31 | + }, |
| 32 | + "archive_allowed_networks": { |
| 33 | + Type: schema.TypeSet, |
| 34 | + Elem: &schema.Schema{ |
| 35 | + Type: schema.TypeString, |
| 36 | + }, |
| 37 | + Description: "Allowed networks for ARCHIVE flow type", |
| 38 | + Computed: true, |
| 39 | + }, |
| 40 | + "direct_input_allowed_networks": { |
| 41 | + Type: schema.TypeSet, |
| 42 | + Elem: &schema.Schema{ |
| 43 | + Type: schema.TypeString, |
| 44 | + }, |
| 45 | + Description: "Allowed networks for DIRECT_INPUT flow type", |
| 46 | + Computed: true, |
| 47 | + }, |
| 48 | + "direct_input_pem": { |
| 49 | + Type: schema.TypeString, |
| 50 | + Description: "PEM for direct inputs", |
| 51 | + Computed: true, |
| 52 | + }, |
| 53 | + "hostname": { |
| 54 | + Type: schema.TypeString, |
| 55 | + Description: "hostname", |
| 56 | + Computed: true, |
| 57 | + }, |
| 58 | + "is_default": { |
| 59 | + Type: schema.TypeBool, |
| 60 | + Description: "All content generated by given service will be placed on this cluster", |
| 61 | + Computed: true, |
| 62 | + }, |
| 63 | + "is_unlocked": { |
| 64 | + Type: schema.TypeBool, |
| 65 | + Description: "Allow given service to perform advanced operations on cluster", |
| 66 | + Computed: true, |
| 67 | + }, |
| 68 | + "query_allowed_networks": { |
| 69 | + Type: schema.TypeSet, |
| 70 | + Elem: &schema.Schema{ |
| 71 | + Type: schema.TypeString, |
| 72 | + }, |
| 73 | + Description: "Allowed networks for QUERY flow type", |
| 74 | + Computed: true, |
| 75 | + }, |
| 76 | + "region": { |
| 77 | + Type: schema.TypeString, |
| 78 | + Description: "Data center localization", |
| 79 | + Computed: true, |
| 80 | + }, |
| 81 | + }, |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func dbaasGetClusterID(config *Config, serviceName string) (string, error) { |
| 86 | + res := []string{} |
| 87 | + |
| 88 | + endpoint := fmt.Sprintf( |
| 89 | + "/dbaas/logs/%s/cluster", |
| 90 | + url.PathEscape(serviceName), |
| 91 | + ) |
| 92 | + |
| 93 | + if err := config.OVHClient.Get(endpoint, &res); err != nil { |
| 94 | + return "", fmt.Errorf("Error calling GET %s:\n\t %q", endpoint, err) |
| 95 | + } |
| 96 | + |
| 97 | + return res[0], nil |
| 98 | +} |
| 99 | + |
| 100 | +func dataSourceDbaasLogsRead(d *schema.ResourceData, meta interface{}) error { |
| 101 | + config := meta.(*Config) |
| 102 | + |
| 103 | + serviceName := d.Get("service_name").(string) |
| 104 | + |
| 105 | + log.Printf("[DEBUG] Will read dbaas logs cluster %s", serviceName) |
| 106 | + |
| 107 | + cluster_id, err := dbaasGetClusterID(config, serviceName) |
| 108 | + |
| 109 | + if err != nil { |
| 110 | + return fmt.Errorf("Error fetching info for %s:\n\t %q", serviceName, err) |
| 111 | + } |
| 112 | + |
| 113 | + d.SetId(cluster_id) |
| 114 | + |
| 115 | + endpoint := fmt.Sprintf( |
| 116 | + "/dbaas/logs/%s/cluster/%s", |
| 117 | + url.PathEscape(serviceName), |
| 118 | + url.PathEscape(cluster_id), |
| 119 | + ) |
| 120 | + |
| 121 | + res := map[string]interface{}{} |
| 122 | + if err := config.OVHClient.Get(endpoint, &res); err != nil { |
| 123 | + return fmt.Errorf("Error calling GET %s:\n\t %q", endpoint, err) |
| 124 | + } |
| 125 | + |
| 126 | + d.Set("archive_allowed_networks", res["archiveAllowedNetworks"]) |
| 127 | + d.Set("cluster_type", res["clusterType"]) |
| 128 | + d.Set("dedicated_input_pem", res["dedicatedInputPEM"]) |
| 129 | + d.Set("direct_input_allowed_networks", res["directInputAllowedNetworks"]) |
| 130 | + d.Set("direct_input_pem", res["directInputPEM"]) |
| 131 | + d.Set("hostname", res["hostname"]) |
| 132 | + d.Set("is_default", res["isDefault"]) |
| 133 | + d.Set("is_unlocked", res["isUnlocked"]) |
| 134 | + d.Set("query_allowed_networks", res["queryAllowedNetworks"]) |
| 135 | + d.Set("region", res["region"]) |
| 136 | + |
| 137 | + return nil |
| 138 | +} |
0 commit comments