-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 7022f31
Showing
14 changed files
with
897 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
terraform-provider-safedns* | ||
.terraform/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2019 UKFast.net Limited | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
testacc: | ||
TF_ACC=1 \ | ||
go test -v \ | ||
./safedns \ | ||
-timeout 120m \ | ||
-run=TestAcc${TEST} | ||
|
||
testacc-all: | ||
TF_ACC=1 \ | ||
go test -v \ | ||
./safedns \ | ||
-timeout 120m |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# terraform-provider-safedns | ||
|
||
## Getting Started | ||
|
||
To get started, the `terraform-provider-safedns` binary (`.exe` extension if Windows) should be downloaded from [Releases](https://github.com/ukfast/terraform-provider-safedns/releases) and placed in a directory. For this example, | ||
we'll place it at `/tmp/terraform-provider-safedns`. | ||
|
||
Next, we'll go ahead and create a new directory to hold our `terraform` file and state: | ||
|
||
```console | ||
mkdir /home/user/terraform | ||
``` | ||
|
||
We'll then create an example terraform file `/home/user/terraform/test.tf`: | ||
|
||
```console | ||
cat <<EOF > /home/user/terraform/test.tf | ||
provider "safedns" { | ||
api_key = "abc" | ||
} | ||
|
||
resource "safedns_zone" "zone-1" { | ||
name = "example.com" | ||
description = "example zone" | ||
} | ||
EOF | ||
``` | ||
|
||
We'll then need to initialise terraform with our provider (specifying `plugin-dir` as the path to where the provider was downloaded to earlier): | ||
|
||
```console | ||
terraform init -get-plugins=false -plugin-dir=/tmp/terraform-provider-safedns | ||
``` | ||
|
||
Finally, we can invoke `terraform apply` to apply our terraform configuration: | ||
|
||
```console | ||
terraform apply | ||
``` | ||
|
||
## Provider | ||
|
||
**Parameters** | ||
|
||
- `api_key`: UKFast API key - read/write permissions for `safedns` service required. If omitted, will use `UKF_API_KEY` environment variable value | ||
|
||
## Resources | ||
|
||
### safedns_zone | ||
|
||
**Schema** | ||
|
||
- `name`: (Required) Name of zone e.g. `example.com` | ||
- `description`: Description for zone | ||
|
||
### safedns_record | ||
|
||
**Schema** | ||
|
||
- `name`: (Required) Name of record e.g. `something.example.com` | ||
- `zone_name`: (Required) Name of zone for record e.g. `example.com` | ||
- `type`: (Required) Type of record | ||
- `content`: (Required) Content for record | ||
- `priority`: Priority of record |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
$env:TF_ACC = "1" | ||
$env:UKF_TEST_ZONE_NAME = Read-Host -Prompt "Enter UKF_TEST_ZONE_NAME" | ||
$env:UKF_TEST_RECORD_NAME = Read-Host -Prompt "Enter UKF_TEST_RECORD_NAME" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module gitlab.devops.ukfast.co.uk/ukfast/api.ukfast/client-libraries/terraform-provider-safedns | ||
|
||
go 1.12 | ||
|
||
require ( | ||
github.com/hashicorp/terraform v0.11.13 | ||
github.com/ukfast/sdk-go v1.0.29 | ||
) |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/hashicorp/terraform/plugin" | ||
"github.com/hashicorp/terraform/terraform" | ||
"gitlab.devops.ukfast.co.uk/ukfast/api.ukfast/client-libraries/terraform-provider-safedns/safedns" | ||
) | ||
|
||
func main() { | ||
plugin.Serve(&plugin.ServeOpts{ | ||
ProviderFunc: func() terraform.ResourceProvider { | ||
return safedns.Provider() | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package safedns | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
|
||
"github.com/hashicorp/terraform/helper/schema" | ||
"github.com/ukfast/sdk-go/pkg/client" | ||
"github.com/ukfast/sdk-go/pkg/connection" | ||
safednsservice "github.com/ukfast/sdk-go/pkg/service/safedns" | ||
) | ||
|
||
func Provider() *schema.Provider { | ||
return &schema.Provider{ | ||
Schema: map[string]*schema.Schema{ | ||
"api_key": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Sensitive: true, | ||
DefaultFunc: func() (interface{}, error) { | ||
key := os.Getenv("UKF_API_KEY") | ||
if key != "" { | ||
return key, nil | ||
} | ||
|
||
return "", errors.New("api_key required") | ||
}, | ||
Description: "API token required to authenticate with UKFast APIs. See https://developers.ukfast.io for more details", | ||
}, | ||
}, | ||
ResourcesMap: map[string]*schema.Resource{ | ||
"safedns_zone": resourceZone(), | ||
"safedns_record": resourceRecord(), | ||
}, | ||
ConfigureFunc: providerConfigure, | ||
} | ||
} | ||
|
||
func providerConfigure(d *schema.ResourceData) (interface{}, error) { | ||
return getService(d.Get("api_key").(string)), nil | ||
} | ||
|
||
func getClient(apiKey string) client.Client { | ||
return client.NewClient(connection.NewAPIKeyCredentialsAPIConnection(apiKey)) | ||
} | ||
|
||
func getService(apiKey string) safednsservice.SafeDNSService { | ||
return getClient(apiKey).SafeDNSService() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package safedns | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/schema" | ||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
var testAccProviders map[string]terraform.ResourceProvider | ||
var testAccProvider *schema.Provider | ||
|
||
var ( | ||
UKF_TEST_ZONE_NAME = os.Getenv("UKF_TEST_ZONE_NAME") | ||
UKF_TEST_RECORD_NAME = os.Getenv("UKF_TEST_RECORD_NAME") | ||
) | ||
|
||
func init() { | ||
testAccProvider = Provider() | ||
testAccProviders = map[string]terraform.ResourceProvider{ | ||
"safedns": testAccProvider, | ||
} | ||
} | ||
|
||
func TestProvider(t *testing.T) { | ||
if err := Provider().InternalValidate(); err != nil { | ||
t.Fatalf("err: %s", err) | ||
} | ||
} | ||
|
||
func TestProvider_impl(t *testing.T) { | ||
var _ terraform.ResourceProvider = Provider() | ||
} | ||
|
||
func testAccPreCheck(t *testing.T) { | ||
testAccPreCheckRequiredEnvVars(t) | ||
} | ||
|
||
func testAccPreCheckRequiredEnvVars(t *testing.T) { | ||
if UKF_TEST_ZONE_NAME == "" { | ||
t.Fatal("UKF_TEST_ZONE_NAME must be set for acceptance tests") | ||
} | ||
if UKF_TEST_RECORD_NAME == "" { | ||
t.Fatal("UKF_TEST_RECORD_NAME must be set for acceptance tests") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package safedns | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"strconv" | ||
|
||
"github.com/hashicorp/terraform/helper/schema" | ||
"github.com/ukfast/sdk-go/pkg/ptr" | ||
safednsservice "github.com/ukfast/sdk-go/pkg/service/safedns" | ||
) | ||
|
||
func resourceRecord() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceRecordCreate, | ||
Read: resourceRecordRead, | ||
Update: resourceRecordUpdate, | ||
Delete: resourceRecordDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"zone_name": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"name": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"type": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"content": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"priority": &schema.Schema{ | ||
Type: schema.TypeInt, | ||
Optional: true, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceRecordCreate(d *schema.ResourceData, meta interface{}) error { | ||
service := meta.(safednsservice.SafeDNSService) | ||
|
||
createReq := safednsservice.CreateRecordRequest{ | ||
Name: d.Get("name").(string), | ||
Type: d.Get("type").(string), | ||
Content: d.Get("content").(string), | ||
} | ||
|
||
priority := d.Get("priority").(int) | ||
if priority > 0 { | ||
createReq.Priority = ptr.Int(priority) | ||
} | ||
log.Printf("Created CreateRecordRequest: %+v", createReq) | ||
|
||
log.Print("Creating record") | ||
recordID, err := service.CreateZoneRecord(d.Get("zone_name").(string), createReq) | ||
if err != nil { | ||
return fmt.Errorf("Error creating record: %s", err) | ||
} | ||
|
||
d.SetId(strconv.Itoa(recordID)) | ||
|
||
return resourceRecordRead(d, meta) | ||
} | ||
|
||
func resourceRecordRead(d *schema.ResourceData, meta interface{}) error { | ||
service := meta.(safednsservice.SafeDNSService) | ||
|
||
zoneName := d.Get("zone_name").(string) | ||
recordID, _ := strconv.Atoi(d.Id()) | ||
|
||
log.Printf("Retrieving record with id [%d] in zone [%s]", recordID, zoneName) | ||
record, err := service.GetZoneRecord(zoneName, recordID) | ||
if err != nil { | ||
switch err.(type) { | ||
case *safednsservice.ZoneRecordNotFoundError: | ||
d.SetId("") | ||
return nil | ||
default: | ||
return err | ||
} | ||
} | ||
|
||
d.Set("name", record.Name) | ||
d.Set("type", record.Type) | ||
d.Set("content", record.Content) | ||
d.Set("priority", record.Priority) | ||
|
||
return nil | ||
} | ||
|
||
func resourceRecordUpdate(d *schema.ResourceData, meta interface{}) error { | ||
service := meta.(safednsservice.SafeDNSService) | ||
|
||
patchRequest := safednsservice.PatchRecordRequest{} | ||
|
||
zoneName := d.Get("zone_name").(string) | ||
recordID, _ := strconv.Atoi(d.Id()) | ||
|
||
if d.HasChange("content") { | ||
patchRequest.Content = d.Get("content").(string) | ||
} | ||
if d.HasChange("priority") { | ||
patchRequest.Priority = ptr.Int(d.Get("priority").(int)) | ||
} | ||
|
||
log.Printf("Updating record with id [%d]", recordID) | ||
_, err := service.PatchZoneRecord(zoneName, recordID, patchRequest) | ||
if err != nil { | ||
return fmt.Errorf("Error updating record with id [%d]", recordID) | ||
} | ||
|
||
return resourceRecordRead(d, meta) | ||
} | ||
|
||
func resourceRecordDelete(d *schema.ResourceData, meta interface{}) error { | ||
service := meta.(safednsservice.SafeDNSService) | ||
|
||
recordID, _ := strconv.Atoi(d.Id()) | ||
|
||
log.Printf("Removing record with id [%d]", recordID) | ||
err := service.DeleteZoneRecord(d.Get("zone_name").(string), recordID) | ||
if err != nil { | ||
return fmt.Errorf("Error removing record with id [%d]: %s", recordID, err) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.