|
| 1 | +package checkly |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "slices" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 9 | + |
| 10 | + checkly "github.com/checkly/checkly-go-sdk" |
| 11 | +) |
| 12 | + |
| 13 | +func dataSourceStaticIPs() *schema.Resource { |
| 14 | + return &schema.Resource{ |
| 15 | + Read: dataSourceStaticIPsRead, |
| 16 | + Schema: map[string]*schema.Schema{ |
| 17 | + "id": { |
| 18 | + Type: schema.TypeString, |
| 19 | + Computed: true, |
| 20 | + Description: "ID of the static IPs data source.", |
| 21 | + }, |
| 22 | + "addresses": { |
| 23 | + Type: schema.TypeSet, |
| 24 | + Elem: &schema.Schema{ |
| 25 | + Type: schema.TypeString, |
| 26 | + }, |
| 27 | + Computed: true, |
| 28 | + Description: "Static IP addresses for Checkly's runner infrastructure.", |
| 29 | + }, |
| 30 | + "locations": { |
| 31 | + Type: schema.TypeSet, |
| 32 | + Elem: &schema.Schema{ |
| 33 | + Type: schema.TypeString, |
| 34 | + }, |
| 35 | + Optional: true, |
| 36 | + Description: "Specify the locations you want to get static IPs for.", |
| 37 | + }, |
| 38 | + "ip_family": { |
| 39 | + Type: schema.TypeString, |
| 40 | + Optional: true, |
| 41 | + Description: "Specify the IP families you want to get static IPs for. Only `IPv6` or `IPv4` are valid options.", |
| 42 | + ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) { |
| 43 | + value := val.(string) |
| 44 | + if !slices.Contains([]string{"IPv6", "IPv4"}, value) { |
| 45 | + errs = append(errs, fmt.Errorf("%q must be either \"IPv6\" or \"IPv4\"", key)) |
| 46 | + } |
| 47 | + return |
| 48 | + }, |
| 49 | + }, |
| 50 | + }, |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func dataSourceStaticIPsRead(d *schema.ResourceData, client interface{}) error { |
| 55 | + ctx, cancel := context.WithTimeout(context.Background(), apiCallTimeout()) |
| 56 | + staticIPs, err := client.(checkly.Client).GetStaticIPs(ctx) |
| 57 | + defer cancel() |
| 58 | + if err != nil { |
| 59 | + return fmt.Errorf("dateSourceStaticIPsRead: API error: %w", err) |
| 60 | + } |
| 61 | + return dataSourceFromStaticIPs(staticIPs, d) |
| 62 | +} |
| 63 | + |
| 64 | +func dataSourceFromStaticIPs(s []checkly.StaticIP, d *schema.ResourceData) error { |
| 65 | + var staticIPs []checkly.StaticIP |
| 66 | + var addresses []string |
| 67 | + |
| 68 | + locations := stringsFromSet(d.Get("locations").(*schema.Set)) |
| 69 | + ip_family := d.Get("ip_family").(string) |
| 70 | + |
| 71 | + // only locations is set for filtering |
| 72 | + if len(locations) > 0 && ip_family == "" { |
| 73 | + for _, ip := range s { |
| 74 | + if slices.Contains(locations, ip.Region) { |
| 75 | + staticIPs = append(staticIPs, ip) |
| 76 | + } |
| 77 | + } |
| 78 | + // only ip_family is set for filtering |
| 79 | + } else if ip_family != "" && len(locations) == 0 { |
| 80 | + for _, ip := range s { |
| 81 | + if ip_family == "IPv4" && ip.Address.Addr().Is4() { |
| 82 | + staticIPs = append(staticIPs, ip) |
| 83 | + } else if ip_family == "IPv6" && ip.Address.Addr().Is6() { |
| 84 | + staticIPs = append(staticIPs, ip) |
| 85 | + } |
| 86 | + } |
| 87 | + // both region and ip_family are set for filtering |
| 88 | + } else if len(locations) > 0 && ip_family != "" { |
| 89 | + for _, ip := range s { |
| 90 | + if ip_family == "IPv4" && ip.Address.Addr().Is4() && slices.Contains(locations, ip.Region) { |
| 91 | + staticIPs = append(staticIPs, ip) |
| 92 | + } else if ip_family == "IPv6" && ip.Address.Addr().Is6() && slices.Contains(locations, ip.Region) { |
| 93 | + staticIPs = append(staticIPs, ip) |
| 94 | + } |
| 95 | + } |
| 96 | + // no region nor ip_family filters set |
| 97 | + } else { |
| 98 | + staticIPs = s |
| 99 | + } |
| 100 | + |
| 101 | + for _, ip := range staticIPs { |
| 102 | + addresses = append(addresses, ip.Address.String()) |
| 103 | + } |
| 104 | + |
| 105 | + d.Set("addresses", addresses) |
| 106 | + d.SetId("checkly_static_ips_data_source_id") |
| 107 | + |
| 108 | + return nil |
| 109 | +} |
0 commit comments