Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/smart records merge on create #52

Merged
merged 10 commits into from
Jul 23, 2021
95 changes: 95 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ on:
jobs:

check:
name: Check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
Expand All @@ -24,6 +25,100 @@ jobs:
uses: golangci/golangci-lint-action@v2
with:
version: v1.41.1
skip-pkg-cache: true

- name: Test
run: make test

start-runner:
name: Start self-hosted EC2 runner
needs: check
runs-on: ubuntu-latest
outputs:
label: ${{ steps.start-ec2-runner.outputs.label }}
ec2-instance-id: ${{ steps.start-ec2-runner.outputs.ec2-instance-id }}
steps:
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.AWS_REGION }}
- name: Start EC2 runner
id: start-ec2-runner
uses: namecheap/ec2-github-runner@main
with:
mode: start
github-token: ${{ secrets.GH_TOKEN }}
ec2-image-owner: 699717368611
ec2-image-filters: >
[
{ "Name": "name", "Values": ["nc-amzn2-ami-hvm-x86_64-gp2-master-*"] }
]
ec2-instance-type: t3.nano
subnet-id: subnet-01c4ff5a
security-group-id: sg-106ec76d
eip-allocation-id: eipalloc-1796f61b
iam-role-name: AmazonSSMRoleForInstancesQuickSetup
aws-resource-tags: >
[
{ "Key": "Name", "Value": "github_runner" },
{ "Key": "GitHubRepository", "Value": "${{ github.repository }}" }
]

acceptance_test:
name: Acceptance test
runs-on: ${{ needs.start-runner.outputs.label }} # run the job on the newly created runner
needs: start-runner
concurrency: acceptance_test # allow to run the only one instance of the current acceptance_test job
steps:
- uses: actions/checkout@v2

- name: Set up Go
run: |
curl https://dl.google.com/go/go1.16.6.linux-amd64.tar.gz -o go1.16.6.linux-amd64.tar.gz
rm -rf .go-instance
mkdir .go-instance
tar -C .go-instance -xzf go1.16.6.linux-amd64.tar.gz
echo "#!/bin/bash" >> go-env.sh
echo "" >> go-env.sh
echo "export PATH=$PATH:$(pwd)/.go-instance/go/bin" >> go-env.sh
echo "export HOME=$(pwd)" >> go-env.sh
chmod +x go-env.sh
source go-env.sh
go version
go env GOPATH

- name: Acceptance Test
run: |
export CGO_ENABLED=0
source go-env.sh
make testacc
env:
NAMECHEAP_USER_NAME: ncexampleuser
NAMECHEAP_API_USER: ncexampleuser
NAMECHEAP_TEST_DOMAIN: test-domain.live
NAMECHEAP_USE_SANDBOX: true
NAMECHEAP_API_KEY: ${{ secrets.NAMECHEAP_API_KEY }}

stop-runner:
name: Stop self-hosted EC2 runner
needs:
- start-runner
- acceptance_test
runs-on: ubuntu-latest
if: ${{ always() }} # required to stop the runner even if the error happened in the previous jobs
steps:
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.AWS_REGION }}
- name: Stop EC2 runner
uses: namecheap/ec2-github-runner@main
with:
mode: stop
github-token: ${{ secrets.GH_TOKEN }}
label: ${{ needs.start-runner.outputs.label }}
ec2-instance-id: ${{ needs.start-runner.outputs.ec2-instance-id }}
41 changes: 41 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Contributing to Terraform - Namecheap Provider

You're welcome to start a discussion about required features, file an issue or submit a work in progress (WIP) pull
request. Feel free to ask us for help. We'll do our best to guide you and help you to get on it.

## Tests

### Running unit tests

To run unit tests, execute the following command:

```shell
$ make test
```

### Running acceptance tests

Before going forward, you must set up the following environment variables:

```dotenv
NAMECHEAP_USER_NAME=user_name
NAMECHEAP_API_USER=user_name
NAMECHEAP_API_KEY=api_key
NAMECHEAP_TEST_DOMAIN=my-domain.com
NAMECHEAP_USE_SANDBOX=true # optional
```

To simplify testing, you can sign up a free account on
our [Sandbox](https://www.namecheap.com/support/knowledgebase/article.aspx/763/63/what-is-sandbox/) environment,
purchase (for free) the fake domain and use the credentials from there for testing environment described below.

**NOTE:** Do not forget to set up `NAMECHEAP_USE_SANDBOX=true` for sandbox account!

**NOTE:** Make sure you have whitelisted your public IP address! Follow
our [API Documentation](https://www.namecheap.com/support/api/intro/) to get info about whitelisting IP.

Run acceptance tests:

```shell
$ make testacc
```
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ check:
go vet ./...

test:
go test -v ./...
go test -v ./... -count=1

# Please set the following ENV variables for this test:
# NAMECHEAP_USER_NAME, NAMECHEAP_API_USER, NAMECHEAP_API_KEY, NAMECHEAP_TEST_DOMAIN, NAMECHEAP_USE_SANDBOX (optional, default is false)
testacc:
TF_ACC=1 go test -v ./... -run=TestAcc -count=1

build:
go build -o ${BINARY}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,4 @@ resource "namecheap_domain_records" "domain2-com" {

### Contributing

You're welcome to post issues and send your pull requests.
To contribute, please read our [contributing](CONTRIBUTING.md) docs.
3 changes: 3 additions & 0 deletions docs/guides/namecheap_domain_records_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ The same workflow works for both: `record` items and `nameservers`.

-> This is the default behavior, however, we recommend to set the mode explicitly.

~> Upon creating a new resource with the records or nameservers that already exist, the provider will throw a duplicate
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to add that it happens if subdomain & record type match

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted. I will check this.
At the moment, we have duplicates comparison only by hostname (subdomain), type, and address.

However, user allowed to create "mistakes" like:

resource "namecheap_domain_records" "domain" {
   #...
   record {
     hostname = "dev"
     type = "A"
     address = "11.22.33.44"
   }

   record {
     hostname = "dev"
     type = "A"
     address = "33.33.33.33"
   }
}

I will amend this behavior (and provide tests for all cases! 😆 )

error.

### `OVERWRITE`

Unlike [MERGE](#merge), `OVERWRITE` always removes existing records and force overwrites with provided in terraform
Expand Down
4 changes: 3 additions & 1 deletion docs/resources/domain_records.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ resource "namecheap_domain_records" "my-domain2-com" {
## Argument Reference

- `domain` - (Required) Purchased available domain name on your account
- `mode` - (Optional) Possible values: `MERGE` (default), `OVERWRITE`.
- `mode` - (Optional) Possible values: `MERGE` (default), `OVERWRITE`
- `email_type` - (Optional) Possible values: NONE, FWD, MXE, MX, OX. Conflicts with `nameservers`
- `record` - (Optional) (see [below for nested schema](#nestedblock--record)) Might contain one or more `record`
records. Conflicts with `nameservers`
Expand All @@ -60,3 +60,5 @@ resource "namecheap_domain_records" "my-domain2-com" {
- `type` - (Required) Possible values: A, AAAA, ALIAS, CAA, CNAME, MX, MXE, NS, TXT, URL, URL301, FRAME
- `mx_pref` - (Optional) MX preference for host. Applicable for MX records only
- `ttl` - (Optional) Time to live for all record types. Possible values: any value between 60 to 60000

~> It is strongly recommended to set `address`, `hostname`, `nameservers` in lower case to prevent undefined behavior!
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module github.com/namecheap/terraform-provider-namecheap

require (
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
github.com/hashicorp/terraform-plugin-sdk/v2 v2.7.0
github.com/namecheap/go-namecheap-sdk/v2 v2.0.1
github.com/stretchr/testify v1.7.0
Expand Down
Loading