|
| 1 | +package checkly |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 9 | + |
| 10 | + checkly "github.com/checkly/checkly-go-sdk" |
| 11 | +) |
| 12 | + |
| 13 | +func resourcePrivateLocation() *schema.Resource { |
| 14 | + return &schema.Resource{ |
| 15 | + Create: resourcePrivateLocationCreate, |
| 16 | + Read: resourcePrivateLocationRead, |
| 17 | + Update: resourcePrivateLocationUpdate, |
| 18 | + Delete: resourcePrivateLocationDelete, |
| 19 | + Importer: &schema.ResourceImporter{ |
| 20 | + StateContext: schema.ImportStatePassthroughContext, |
| 21 | + }, |
| 22 | + Schema: map[string]*schema.Schema{ |
| 23 | + "name": { |
| 24 | + Type: schema.TypeString, |
| 25 | + Required: true, |
| 26 | + Description: "The private location name.", |
| 27 | + }, |
| 28 | + "slug_name": { |
| 29 | + Type: schema.TypeString, |
| 30 | + Required: true, |
| 31 | + Description: "Valid slug name.", |
| 32 | + }, |
| 33 | + "icon": { |
| 34 | + Type: schema.TypeString, |
| 35 | + Optional: true, |
| 36 | + Description: "Icon assigned to the private location.", |
| 37 | + }, |
| 38 | + "keys": { |
| 39 | + Type: schema.TypeSet, |
| 40 | + Computed: true, |
| 41 | + Sensitive: true, |
| 42 | + Elem: &schema.Schema{ |
| 43 | + Type: schema.TypeString, |
| 44 | + }, |
| 45 | + Description: "Private location API keys.", |
| 46 | + }, |
| 47 | + }, |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func privateLocationFromResourceData(d *schema.ResourceData) (checkly.PrivateLocation, error) { |
| 52 | + return checkly.PrivateLocation{ |
| 53 | + Name: d.Get("name").(string), |
| 54 | + SlugName: d.Get("slug_name").(string), |
| 55 | + Icon: d.Get("icon").(string), |
| 56 | + }, nil |
| 57 | +} |
| 58 | + |
| 59 | +func resourcePrivateLocationCreate(d *schema.ResourceData, client interface{}) error { |
| 60 | + pl, err := privateLocationFromResourceData(d) |
| 61 | + if err != nil { |
| 62 | + return fmt.Errorf("resourcePrivateLocationCreate: translation error: %w", err) |
| 63 | + } |
| 64 | + ctx, cancel := context.WithTimeout(context.Background(), apiCallTimeout()) |
| 65 | + defer cancel() |
| 66 | + result, err := client.(checkly.Client).CreatePrivateLocation(ctx, pl) |
| 67 | + if err != nil { |
| 68 | + return fmt.Errorf("CreatePrivateLocation: API error: %w", err) |
| 69 | + } |
| 70 | + d.SetId(result.ID) |
| 71 | + |
| 72 | + var keys = []string{result.Keys[0].RawKey} |
| 73 | + d.Set("keys", keys) |
| 74 | + return resourcePrivateLocationRead(d, client) |
| 75 | +} |
| 76 | + |
| 77 | +func resourceDataFromPrivateLocation(pl *checkly.PrivateLocation, d *schema.ResourceData) error { |
| 78 | + d.Set("name", pl.Name) |
| 79 | + d.Set("slug_name", pl.SlugName) |
| 80 | + d.Set("icon", pl.Icon) |
| 81 | + return nil |
| 82 | +} |
| 83 | + |
| 84 | +func resourcePrivateLocationUpdate(d *schema.ResourceData, client interface{}) error { |
| 85 | + pl, err := privateLocationFromResourceData(d) |
| 86 | + if err != nil { |
| 87 | + return fmt.Errorf("resourcePrivateLocationUpdate: translation error: %w", err) |
| 88 | + } |
| 89 | + ctx, cancel := context.WithTimeout(context.Background(), apiCallTimeout()) |
| 90 | + defer cancel() |
| 91 | + _, err = client.(checkly.Client).UpdatePrivateLocation(ctx, d.Id(), pl) |
| 92 | + if err != nil { |
| 93 | + return fmt.Errorf("resourcePrivateLocationUpdate: API error: %w", err) |
| 94 | + } |
| 95 | + return resourcePrivateLocationRead(d, client) |
| 96 | +} |
| 97 | + |
| 98 | +func resourcePrivateLocationRead(d *schema.ResourceData, client interface{}) error { |
| 99 | + ctx, cancel := context.WithTimeout(context.Background(), apiCallTimeout()) |
| 100 | + defer cancel() |
| 101 | + pl, err := client.(checkly.Client).GetPrivateLocation(ctx, d.Id()) |
| 102 | + if err != nil { |
| 103 | + if strings.Contains(err.Error(), "404") { |
| 104 | + //if resource is deleted remotely, then mark it as |
| 105 | + //successfully gone by unsetting it's ID |
| 106 | + d.SetId("") |
| 107 | + return nil |
| 108 | + } |
| 109 | + return fmt.Errorf("resourcePrivateLocationRead: %w", err) |
| 110 | + } |
| 111 | + return resourceDataFromPrivateLocation(pl, d) |
| 112 | +} |
| 113 | + |
| 114 | +func resourcePrivateLocationDelete(d *schema.ResourceData, client interface{}) error { |
| 115 | + ctx, cancel := context.WithTimeout(context.Background(), apiCallTimeout()) |
| 116 | + defer cancel() |
| 117 | + err := client.(checkly.Client).DeletePrivateLocation(ctx, d.Id()) |
| 118 | + if err != nil { |
| 119 | + return fmt.Errorf("resourcePrivateLocationDelete: API error: %w", err) |
| 120 | + } |
| 121 | + return nil |
| 122 | +} |
0 commit comments