|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strconv" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" |
| 12 | + |
| 13 | + statuscake "github.com/StatusCakeDev/statuscake-go" |
| 14 | +) |
| 15 | + |
| 16 | +type monitoringLocationsFunc func(context.Context, *statuscake.Client, string) (statuscake.MonitoringLocations, error) |
| 17 | + |
| 18 | +func dataSourceStatusCakeMonitoringLocations(fn monitoringLocationsFunc) *schema.Resource { |
| 19 | + return &schema.Resource{ |
| 20 | + ReadContext: dataSourceStatusCakeUptimeLocationsRead(fn), |
| 21 | + |
| 22 | + Schema: map[string]*schema.Schema{ |
| 23 | + "region_code": &schema.Schema{ |
| 24 | + Type: schema.TypeString, |
| 25 | + Optional: true, |
| 26 | + Description: "Location region code", |
| 27 | + ValidateFunc: validation.StringIsNotEmpty, |
| 28 | + }, |
| 29 | + "locations": &schema.Schema{ |
| 30 | + Type: schema.TypeList, |
| 31 | + Computed: true, |
| 32 | + Description: "List of monitoring locations", |
| 33 | + Elem: &schema.Resource{ |
| 34 | + Schema: locationSchema(), |
| 35 | + }, |
| 36 | + }, |
| 37 | + }, |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +// locationsSchema returns the schema describing a single monitoring locations. |
| 42 | +// Since locations features within multiple resources its structure has been |
| 43 | +// encapsulated within a function. |
| 44 | +func locationSchema() map[string]*schema.Schema { |
| 45 | + return map[string]*schema.Schema{ |
| 46 | + "description": &schema.Schema{ |
| 47 | + Type: schema.TypeString, |
| 48 | + Computed: true, |
| 49 | + Description: "Location description", |
| 50 | + }, |
| 51 | + "ipv4": &schema.Schema{ |
| 52 | + Type: schema.TypeString, |
| 53 | + Computed: true, |
| 54 | + Description: "Location IPv4 address", |
| 55 | + }, |
| 56 | + "ipv6": &schema.Schema{ |
| 57 | + Type: schema.TypeString, |
| 58 | + Computed: true, |
| 59 | + Description: "Location IPv6 address", |
| 60 | + }, |
| 61 | + "region": &schema.Schema{ |
| 62 | + Type: schema.TypeString, |
| 63 | + Computed: true, |
| 64 | + Description: "Location region", |
| 65 | + }, |
| 66 | + "region_code": &schema.Schema{ |
| 67 | + Type: schema.TypeString, |
| 68 | + Computed: true, |
| 69 | + Description: "Location region code", |
| 70 | + }, |
| 71 | + "status": &schema.Schema{ |
| 72 | + Type: schema.TypeString, |
| 73 | + Computed: true, |
| 74 | + Description: "Location status", |
| 75 | + }, |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +func dataSourceStatusCakeUptimeLocationsRead(fn monitoringLocationsFunc) schema.ReadContextFunc { |
| 80 | + return func(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 81 | + client := meta.(*statuscake.Client) |
| 82 | + |
| 83 | + res, err := fn(ctx, client, d.Get("region_code").(string)) |
| 84 | + if err != nil { |
| 85 | + return diag.FromErr(fmt.Errorf("failed to list monitoring locations: %w", err)) |
| 86 | + } |
| 87 | + |
| 88 | + if err := d.Set("locations", flattenMonitoringLocations(res.Data, d)); err != nil { |
| 89 | + return diag.FromErr(fmt.Errorf("error setting monitoring locations: %w", err)) |
| 90 | + } |
| 91 | + |
| 92 | + d.SetId(strconv.FormatInt(time.Now().Unix(), 10)) |
| 93 | + |
| 94 | + return nil |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +func listUptimeMonitoringLocations(ctx context.Context, client *statuscake.Client, location string) (statuscake.MonitoringLocations, error) { |
| 99 | + req := client.ListUptimeMonitoringLocations(ctx) |
| 100 | + |
| 101 | + if len(location) != 0 { |
| 102 | + req = req.Location(location) |
| 103 | + } |
| 104 | + |
| 105 | + return req.Execute() |
| 106 | +} |
| 107 | + |
| 108 | +func listPagespeedMonitoringLocations(ctx context.Context, client *statuscake.Client, location string) (statuscake.MonitoringLocations, error) { |
| 109 | + req := client.ListPagespeedMonitoringLocations(ctx) |
| 110 | + |
| 111 | + if len(location) != 0 { |
| 112 | + req = req.Location(location) |
| 113 | + } |
| 114 | + |
| 115 | + return req.Execute() |
| 116 | +} |
| 117 | + |
| 118 | +func flattenMonitoringLocationsDescription(v interface{}, d *schema.ResourceData) interface{} { |
| 119 | + return v |
| 120 | +} |
| 121 | + |
| 122 | +func flattenMonitoringLocationsIPv4(v interface{}, d *schema.ResourceData) interface{} { |
| 123 | + return v |
| 124 | +} |
| 125 | + |
| 126 | +func flattenMonitoringLocationsIPv6(v interface{}, d *schema.ResourceData) interface{} { |
| 127 | + return v |
| 128 | +} |
| 129 | + |
| 130 | +func flattenMonitoringLocations(v interface{}, d *schema.ResourceData) interface{} { |
| 131 | + data := v.([]statuscake.MonitoringLocation) |
| 132 | + |
| 133 | + locations := make([]interface{}, len(data)) |
| 134 | + for idx, location := range data { |
| 135 | + locations[idx] = flattenMonitoringLocation(location, d) |
| 136 | + } |
| 137 | + |
| 138 | + return locations |
| 139 | +} |
| 140 | + |
| 141 | +func flattenMonitoringLocation(v interface{}, d *schema.ResourceData) interface{} { |
| 142 | + data := v.(statuscake.MonitoringLocation) |
| 143 | + |
| 144 | + return map[string]interface{}{ |
| 145 | + "description": flattenMonitoringLocationsDescription(data.Description, d), |
| 146 | + "ipv4": flattenMonitoringLocationsIPv4(data.IPv4, d), |
| 147 | + "ipv6": flattenMonitoringLocationsIPv6(data.IPv6, d), |
| 148 | + "region": flattenMonitoringLocationsRegion(data.Region, d), |
| 149 | + "region_code": flattenMonitoringLocationsRegionCode(data.RegionCode, d), |
| 150 | + "status": flattenMonitoringLocationsStatus(data.Status, d), |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +func flattenMonitoringLocationsRegion(v interface{}, d *schema.ResourceData) interface{} { |
| 155 | + return v |
| 156 | +} |
| 157 | + |
| 158 | +func flattenMonitoringLocationsRegionCode(v interface{}, d *schema.ResourceData) interface{} { |
| 159 | + return v |
| 160 | +} |
| 161 | + |
| 162 | +func flattenMonitoringLocationsStatus(v interface{}, d *schema.ResourceData) interface{} { |
| 163 | + return v |
| 164 | +} |
0 commit comments