Skip to content
This repository was archived by the owner on Mar 1, 2023. It is now read-only.

Commit 9e0314a

Browse files
authored
Add dns (#43)
* + dns zone resource * + dns zone record resource * ~ check id * fix zone record, add docs, add tests * add test on update * mv client to sdk * fix creating records with unexisted zone * rename resource_records to resource_record * add filter to records * add filter to records
1 parent 4f1f860 commit 9e0314a

File tree

14 files changed

+1069
-26
lines changed

14 files changed

+1069
-26
lines changed

docs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ resource "gcore_lbmember" "lbm2" {
247247
- **gcore_api** (String) Region API
248248
- **gcore_cdn_api** (String) CDN API
249249
- **gcore_client_id** (String) Client id
250+
- **gcore_dns_api** (String) DNS API
250251
- **gcore_platform** (String) Platform ulr is used for generate jwt
251252
- **gcore_storage_api** (String) Storage API
252253
- **ignore_creds_auth_error** (Boolean) Should be set to true when you are gonna to use storage resource with permanent API-token only.

docs/resources/gcore_dns_zone.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "gcore_dns_zone Resource - terraform-provider-gcorelabs"
4+
subcategory: ""
5+
description: |-
6+
Represent DNS zone resource. https://dns.gcorelabs.com/zones
7+
---
8+
9+
# gcore_dns_zone (Resource)
10+
11+
Represent DNS zone resource. https://dns.gcorelabs.com/zones
12+
13+
## Example Usage
14+
15+
```terraform
16+
provider gcore {
17+
user_name = "test"
18+
password = "test"
19+
permanent_api_token="123$321" // https://support.gcorelabs.com/hc/en-us/articles/360018625617-API-tokens
20+
ignore_creds_auth_error=true // if you want to manage storage or dns resources only and provide permanent_api_token without user_name & password
21+
gcore_platform = "https://api.gcdn.co"
22+
gcore_dns_api = "https://dnsapi.gcorelabs.com"
23+
}
24+
25+
resource "gcore_dns_zone" "example_zone" {
26+
name = "example_zone.com"
27+
}
28+
```
29+
30+
<!-- schema generated by tfplugindocs -->
31+
## Schema
32+
33+
### Required
34+
35+
- **name** (String) A name of DNS Zone resource.
36+
37+
### Optional
38+
39+
- **id** (String) The ID of this resource.
40+
41+
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "gcore_dns_zone_record Resource - terraform-provider-gcorelabs"
4+
subcategory: ""
5+
description: |-
6+
Represent DNS Zone Record resource. https://dns.gcorelabs.com/zones
7+
---
8+
9+
# gcore_dns_zone_record (Resource)
10+
11+
Represent DNS Zone Record resource. https://dns.gcorelabs.com/zones
12+
13+
## Example Usage
14+
15+
```terraform
16+
provider gcore {
17+
user_name = "test"
18+
password = "test"
19+
permanent_api_token="123$321" // https://support.gcorelabs.com/hc/en-us/articles/360018625617-API-tokens
20+
ignore_creds_auth_error=true // if you want to manage storage or dns resources only and provide permanent_api_token without user_name & password
21+
gcore_platform = "https://api.gcdn.co"
22+
gcore_dns_api = "https://dnsapi.gcorelabs.com"
23+
}
24+
25+
resource "gcore_dns_zone_record" "subdomain_examplezone" {
26+
zone = "examplezone.com"
27+
domain = "subdomain.examplezone.com"
28+
type = "TXT"
29+
ttl = 10
30+
31+
filter {
32+
type = "geodistance"
33+
limit = 1
34+
strict = true
35+
}
36+
37+
resource_record {
38+
content = "1234"
39+
enabled = true
40+
41+
meta {
42+
latlong = [52.367,4.9041]
43+
asn = [12345]
44+
ip = ["1.1.1.1"]
45+
notes = ["notes"]
46+
continents = ["asia"]
47+
countries = ["russia"]
48+
default = true
49+
}
50+
}
51+
}
52+
```
53+
54+
<!-- schema generated by tfplugindocs -->
55+
## Schema
56+
57+
### Required
58+
59+
- **domain** (String) A domain of DNS Zone Record resource.
60+
- **resource_record** (Block Set, Min: 1) An array of contents with meta of DNS Zone Record resource. (see [below for nested schema](#nestedblock--resource_record))
61+
- **type** (String) A type of DNS Zone Record resource.
62+
- **zone** (String) A zone of DNS Zone Record resource.
63+
64+
### Optional
65+
66+
- **filter** (Block Set) (see [below for nested schema](#nestedblock--filter))
67+
- **id** (String) The ID of this resource.
68+
- **ttl** (Number) A ttl of DNS Zone Record resource.
69+
70+
<a id="nestedblock--resource_record"></a>
71+
### Nested Schema for `resource_record`
72+
73+
Required:
74+
75+
- **content** (String) A content of DNS Zone Record resource.
76+
77+
Optional:
78+
79+
- **enabled** (Boolean) Manage of public appearing of DNS Zone Record resource.
80+
- **meta** (Block Set, Max: 1) (see [below for nested schema](#nestedblock--resource_record--meta))
81+
82+
<a id="nestedblock--resource_record--meta"></a>
83+
### Nested Schema for `resource_record.meta`
84+
85+
Optional:
86+
87+
- **asn** (List of Number) An asn meta (e.g. 12345) of DNS Zone Record resource.
88+
- **continents** (List of String) Continents meta (e.g. Asia) of DNS Zone Record resource.
89+
- **countries** (List of String) Countries meta (e.g. USA) of DNS Zone Record resource.
90+
- **default** (Boolean) Fallback meta equals true marks records which are used as a default answer (when nothing was selected by specified meta fields).
91+
- **ip** (List of String) An ip meta (e.g. 127.0.0.0) of DNS Zone Record resource.
92+
- **latlong** (List of Number) A latlong meta (e.g. 27.988056, 86.925278) of DNS Zone Record resource.
93+
- **notes** (List of String) A notes meta (e.g. Miami DC) of DNS Zone Record resource.
94+
95+
96+
97+
<a id="nestedblock--filter"></a>
98+
### Nested Schema for `filter`
99+
100+
Required:
101+
102+
- **type** (String) A DNS Zone Record filter option that describe a name of filter.
103+
104+
Optional:
105+
106+
- **limit** (Number) A DNS Zone Record filter option that describe how many records will be percolated.
107+
- **strict** (Boolean) A DNS Zone Record filter option that describe possibility to return answers if no records were percolated through filter.
108+
109+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
provider gcore {
2+
user_name = "test"
3+
password = "test"
4+
permanent_api_token="123$321" // https://support.gcorelabs.com/hc/en-us/articles/360018625617-API-tokens
5+
ignore_creds_auth_error=true // if you want to manage storage or dns resources only and provide permanent_api_token without user_name & password
6+
gcore_platform = "https://api.gcdn.co"
7+
gcore_dns_api = "https://dnsapi.gcorelabs.com"
8+
}
9+
10+
resource "gcore_dns_zone" "example_zone" {
11+
name = "example_zone.com"
12+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
provider gcore {
2+
user_name = "test"
3+
password = "test"
4+
permanent_api_token="123$321" // https://support.gcorelabs.com/hc/en-us/articles/360018625617-API-tokens
5+
ignore_creds_auth_error=true // if you want to manage storage or dns resources only and provide permanent_api_token without user_name & password
6+
gcore_platform = "https://api.gcdn.co"
7+
gcore_dns_api = "https://dnsapi.gcorelabs.com"
8+
}
9+
10+
resource "gcore_dns_zone_record" "subdomain_examplezone" {
11+
zone = "examplezone.com"
12+
domain = "subdomain.examplezone.com"
13+
type = "TXT"
14+
ttl = 10
15+
16+
filter {
17+
type = "geodistance"
18+
limit = 1
19+
strict = true
20+
}
21+
22+
resource_record {
23+
content = "1234"
24+
enabled = true
25+
26+
meta {
27+
latlong = [52.367,4.9041]
28+
asn = [12345]
29+
ip = ["1.1.1.1"]
30+
notes = ["notes"]
31+
continents = ["asia"]
32+
countries = ["russia"]
33+
default = true
34+
}
35+
}
36+
}

gcore/provider.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import (
55
"fmt"
66
"log"
77
"net/http"
8+
"net/url"
89

10+
dnssdk "github.com/G-Core/g-dns-sdk-go"
911
storageSDK "github.com/G-Core/gcorelabs-storage-sdk-go"
1012
gcdn "github.com/G-Core/gcorelabscdn-go"
1113
gcdnProvider "github.com/G-Core/gcorelabscdn-go/gcore/provider"
@@ -72,6 +74,12 @@ func Provider() *schema.Provider {
7274
Description: "Storage API",
7375
DefaultFunc: schema.EnvDefaultFunc("GCORE_STORAGE_API", ""),
7476
},
77+
"gcore_dns_api": {
78+
Type: schema.TypeString,
79+
Optional: true,
80+
Description: "DNS API",
81+
DefaultFunc: schema.EnvDefaultFunc("GCORE_DNS_API", ""),
82+
},
7583
"gcore_client_id": &schema.Schema{
7684
Type: schema.TypeString,
7785
Optional: true,
@@ -99,6 +107,8 @@ func Provider() *schema.Provider {
99107
"gcore_k8s": resourceK8s(),
100108
"gcore_k8s_pool": resourceK8sPool(),
101109
"gcore_storage_s3": resourceStorageS3(),
110+
DNSZoneResource: resourceDNSZone(),
111+
DNSZoneRecordResource: resourceDNSZoneRecord(),
102112
"gcore_storage_sftp": resourceStorageSFTP(),
103113
"gcore_storage_sftp_key": resourceStorageSFTPKey(),
104114
"gcore_cdn_resource": resourceCDNResource(),
@@ -140,6 +150,7 @@ func providerConfigure(ctx context.Context, d *schema.ResourceData) (interface{}
140150
api := d.Get("gcore_api").(string)
141151
cdnAPI := d.Get("gcore_cdn_api").(string)
142152
storageAPI := d.Get("gcore_storage_api").(string)
153+
dnsAPI := d.Get("gcore_dns_api").(string)
143154
platform := d.Get("gcore_platform").(string)
144155
clientID := d.Get("gcore_client_id").(string)
145156

@@ -187,6 +198,19 @@ func providerConfigure(ctx context.Context, d *schema.ResourceData) (interface{}
187198
storageSDK.WithPermanentTokenAuth(func() string { return permanentToken }),
188199
)
189200
}
201+
if dnsAPI != "" {
202+
baseUrl, err := url.Parse(dnsAPI)
203+
if err != nil {
204+
return nil, diag.FromErr(fmt.Errorf("dns api url: %w", err))
205+
}
206+
authorizer := dnssdk.BearerAuth(provider.AccessToken())
207+
if permanentToken != "" {
208+
authorizer = dnssdk.PermanentAPIKeyAuth(permanentToken)
209+
}
210+
config.DNSClient = dnssdk.NewClient(authorizer, func(client *dnssdk.Client) {
211+
client.BaseURL = baseUrl
212+
})
213+
}
190214

191215
return &config, diags
192216
}

gcore/provider_test.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@ package gcore
33
import (
44
"fmt"
55
"net/http"
6+
"net/url"
67
"os"
78
"strconv"
89
"strings"
910
"testing"
1011

12+
dnssdk "github.com/G-Core/g-dns-sdk-go"
1113
storageSDK "github.com/G-Core/gcorelabs-storage-sdk-go"
12-
13-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
14-
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
15-
1614
gcdn "github.com/G-Core/gcorelabscdn-go"
1715
gcdnProvider "github.com/G-Core/gcorelabscdn-go/gcore/provider"
1816
gcorecloud "github.com/G-Core/gcorelabscloud-go"
1917
gc "github.com/G-Core/gcorelabscloud-go/gcore"
18+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
2019
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
20+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
2121
)
2222

2323
type VarName string
@@ -27,6 +27,7 @@ const (
2727
GCORE_PASSWORD_VAR VarName = "GCORE_PASSWORD"
2828
GCORE_CDN_URL_VAR VarName = "GCORE_CDN_URL"
2929
GCORE_STORAGE_URL_VAR VarName = "GCORE_STORAGE_API"
30+
GCORE_DNS_URL_VAR VarName = "GCORE_DNS_API"
3031
GCORE_IMAGE_VAR VarName = "GCORE_IMAGE"
3132
GCORE_SECGROUP_VAR VarName = "GCORE_SECGROUP"
3233
GCORE_EXT_NET_VAR VarName = "GCORE_EXT_NET"
@@ -64,6 +65,7 @@ var (
6465
GCORE_CDN_ORIGINGROUP_ID = getEnv(GCORE_CDN_ORIGINGROUP_ID_VAR)
6566
GCORE_CDN_RESOURCE_ID = getEnv(GCORE_CDN_RESOURCE_ID_VAR)
6667
GCORE_STORAGE_API = getEnv(GCORE_STORAGE_URL_VAR)
68+
GCORE_DNS_API = getEnv(GCORE_DNS_URL_VAR)
6769
GCORE_NETWORK_ID = getEnv(GCORE_NETWORK_ID_VAR)
6870
GCORE_SUBNET_ID = getEnv(GCORE_SUBNET_ID_VAR)
6971
GCORE_CLUSTER_ID = getEnv(GCORE_CLUSTER_ID_VAR)
@@ -86,6 +88,7 @@ var varsMap = map[VarName]string{
8688
GCORE_CDN_ORIGINGROUP_ID_VAR: GCORE_CDN_ORIGINGROUP_ID,
8789
GCORE_CDN_RESOURCE_ID_VAR: GCORE_CDN_RESOURCE_ID,
8890
GCORE_STORAGE_URL_VAR: GCORE_STORAGE_API,
91+
GCORE_DNS_URL_VAR: GCORE_DNS_API,
8992
GCORE_NETWORK_ID_VAR: GCORE_NETWORK_ID,
9093
GCORE_SUBNET_ID_VAR: GCORE_SUBNET_ID,
9194
GCORE_CLUSTER_ID_VAR: GCORE_CLUSTER_ID,
@@ -359,10 +362,23 @@ func createTestConfig() (*Config, error) {
359362
storageClient = storageSDK.NewSDK(stHost, stPath, storageSDK.WithBearerAuth(provider.AccessToken))
360363
}
361364

365+
var dnsClient *dnssdk.Client
366+
if GCORE_DNS_API != "" {
367+
baseUrl, err := url.Parse(GCORE_DNS_API)
368+
if err == nil {
369+
authorizer := dnssdk.BearerAuth(provider.AccessToken())
370+
dnsClient = dnssdk.NewClient(authorizer, func(client *dnssdk.Client) {
371+
client.BaseURL = baseUrl
372+
})
373+
}
374+
375+
}
376+
362377
config := Config{
363378
Provider: provider,
364379
CDNClient: cdnService,
365380
StorageClient: storageClient,
381+
DNSClient: dnsClient,
366382
}
367383

368384
return &config, err

0 commit comments

Comments
 (0)