-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added entities for work with DNS v2 API (#249)
Added selectel_domains_zone_v2 resource. Added selectel_domains_rrset_v2 resource.
- Loading branch information
Showing
19 changed files
with
1,434 additions
and
17 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
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
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,157 @@ | ||
package selectel | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
domainsV2 "github.com/selectel/domains-go/pkg/v2" | ||
) | ||
|
||
var ErrProjectIDNotSetupForDNSV2 = errors.New("env variable SEL_PROJECT_ID or variable project_id must be set for the dns v2") | ||
|
||
func getDomainsV2Client(d *schema.ResourceData, meta interface{}) (domainsV2.DNSClient[domainsV2.Zone, domainsV2.RRSet], error) { | ||
config := meta.(*Config) | ||
projectID := d.Get("project_id").(string) | ||
selvpcClient, err := config.GetSelVPCClientWithProjectScope(projectID) | ||
if err != nil { | ||
return nil, fmt.Errorf("can't get selvpc client for domains v2: %w", err) | ||
} | ||
|
||
httpClient := &http.Client{} | ||
userAgent := "terraform-provider-selectel" | ||
defaultAPIURL := "https://api.selectel.ru/domains/v2" | ||
hdrs := http.Header{} | ||
hdrs.Add("X-Auth-Token", selvpcClient.GetXAuthToken()) | ||
hdrs.Add("User-Agent", userAgent) | ||
domainsClient := domainsV2.NewClient(defaultAPIURL, httpClient, hdrs) | ||
|
||
return domainsClient, nil | ||
} | ||
|
||
func getZoneByName(ctx context.Context, client domainsV2.DNSClient[domainsV2.Zone, domainsV2.RRSet], zoneName string) (*domainsV2.Zone, error) { | ||
optsForSearchZone := map[string]string{ | ||
"filter": zoneName, | ||
"limit": "1000", | ||
"offset": "0", | ||
} | ||
r, err := regexp.Compile(fmt.Sprintf("^%s.?", zoneName)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for { | ||
zones, err := client.ListZones(ctx, &optsForSearchZone) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, zone := range zones.GetItems() { | ||
if r.MatchString(zone.Name) { | ||
return zone, nil | ||
} | ||
} | ||
optsForSearchZone["offset"] = strconv.Itoa(zones.GetNextOffset()) | ||
if zones.GetNextOffset() == 0 { | ||
break | ||
} | ||
} | ||
|
||
return nil, errGettingObject(objectZone, zoneName, ErrZoneNotFound) | ||
} | ||
|
||
func getRRSetByNameAndType(ctx context.Context, client domainsV2.DNSClient[domainsV2.Zone, domainsV2.RRSet], zoneID, rrsetName, rrsetType string) (*domainsV2.RRSet, error) { | ||
optsForSearchRRSet := map[string]string{ | ||
"name": rrsetName, | ||
"rrset_types": rrsetType, | ||
"limit": "1000", | ||
"offset": "0", | ||
} | ||
|
||
r, err := regexp.Compile(fmt.Sprintf("^%s.?", rrsetName)) | ||
if err != nil { | ||
return nil, errGettingObject(objectRRSet, rrsetName, err) | ||
} | ||
|
||
for { | ||
rrsets, err := client.ListRRSets(ctx, zoneID, &optsForSearchRRSet) | ||
if err != nil { | ||
return nil, errGettingObject(objectRRSet, rrsetName, err) | ||
} | ||
for _, rrset := range rrsets.GetItems() { | ||
if r.MatchString(rrset.Name) && string(rrset.Type) == rrsetType { | ||
return rrset, nil | ||
} | ||
} | ||
optsForSearchRRSet["offset"] = strconv.Itoa(rrsets.GetNextOffset()) | ||
if rrsets.GetNextOffset() == 0 { | ||
break | ||
} | ||
} | ||
|
||
return nil, errGettingObject(objectRRSet, fmt.Sprintf("Name: %s. Type: %s.", rrsetName, rrsetType), ErrRRSetNotFound) | ||
} | ||
|
||
func setZoneToResourceData(d *schema.ResourceData, zone *domainsV2.Zone) error { | ||
d.SetId(zone.ID) | ||
d.Set("name", zone.Name) | ||
d.Set("comment", zone.Comment) | ||
d.Set("created_at", zone.CreatedAt.Format(time.RFC3339)) | ||
d.Set("updated_at", zone.UpdatedAt.Format(time.RFC3339)) | ||
d.Set("delegation_checked_at", zone.DelegationCheckedAt.Format(time.RFC3339)) | ||
d.Set("last_check_status", zone.LastCheckStatus) | ||
d.Set("last_delegated_at", zone.LastDelegatedAt.Format(time.RFC3339)) | ||
d.Set("project_id", strings.ReplaceAll(zone.ProjectID, "-", "")) | ||
d.Set("disabled", zone.Disabled) | ||
|
||
return nil | ||
} | ||
|
||
func setRRSetToResourceData(d *schema.ResourceData, rrset *domainsV2.RRSet) error { | ||
d.SetId(rrset.ID) | ||
d.Set("name", rrset.Name) | ||
d.Set("comment", rrset.Comment) | ||
d.Set("managed_by", rrset.ManagedBy) | ||
d.Set("ttl", rrset.TTL) | ||
d.Set("type", rrset.Type) | ||
d.Set("zone_id", rrset.ZoneID) | ||
d.Set("records", generateSetFromRecords(rrset.Records)) | ||
|
||
return nil | ||
} | ||
|
||
// generateSetFromRecords - generate terraform TypeList from records in RRSet. | ||
func generateSetFromRecords(records []domainsV2.RecordItem) []interface{} { | ||
recordsAsList := []interface{}{} | ||
for _, record := range records { | ||
recordsAsList = append(recordsAsList, map[string]interface{}{ | ||
"content": record.Content, | ||
"disabled": record.Disabled, | ||
}) | ||
} | ||
|
||
return recordsAsList | ||
} | ||
|
||
// generateRecordsFromSet - generate records for RRSet from terraform TypeList. | ||
func generateRecordsFromSet(recordsSet *schema.Set) []domainsV2.RecordItem { | ||
records := []domainsV2.RecordItem{} | ||
for _, recordItem := range recordsSet.List() { | ||
record, isOk := recordItem.(map[string]interface{}) | ||
if !isOk { | ||
continue | ||
} | ||
records = append(records, domainsV2.RecordItem{ | ||
Content: record["content"].(string), | ||
Disabled: record["disabled"].(bool), | ||
}) | ||
} | ||
|
||
return records | ||
} |
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,166 @@ | ||
package selectel | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform" | ||
domainsV2 "github.com/selectel/domains-go/pkg/v2" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
) | ||
|
||
func getDomainsV2ClientTest(rs *terraform.ResourceState, testAccProvider *schema.Provider) (domainsV2.DNSClient[domainsV2.Zone, domainsV2.RRSet], error) { | ||
config := testAccProvider.Meta().(*Config) | ||
projectID, ok := rs.Primary.Attributes["project_id"] | ||
if !ok { | ||
return nil, ErrProjectIDNotSetupForDNSV2 | ||
} | ||
selvpcClient, err := config.GetSelVPCClientWithProjectScope(projectID) | ||
if err != nil { | ||
return nil, fmt.Errorf("can't get selvpc client for domains v2: %w", err) | ||
} | ||
|
||
httpClient := &http.Client{} | ||
userAgent := "terraform-provider-selectel" | ||
defaultAPIURL := "https://api.selectel.ru/domains/v2" | ||
hdrs := http.Header{} | ||
hdrs.Add("X-Auth-Token", selvpcClient.GetXAuthToken()) | ||
hdrs.Add("User-Agent", userAgent) | ||
domainsClient := domainsV2.NewClient(defaultAPIURL, httpClient, hdrs) | ||
|
||
return domainsClient, nil | ||
} | ||
|
||
type mockedDNSv2Client struct { | ||
mock.Mock | ||
domainsV2.Client | ||
} | ||
|
||
func (client *mockedDNSv2Client) ListZones(ctx context.Context, opts *map[string]string) (domainsV2.Listable[domainsV2.Zone], error) { | ||
args := client.Called(ctx, opts) | ||
zones := args.Get(0).(domainsV2.Listable[domainsV2.Zone]) | ||
err := args.Error(1) | ||
|
||
return zones, err | ||
} | ||
|
||
func (client *mockedDNSv2Client) ListRRSets(ctx context.Context, zoneID string, opts *map[string]string) (domainsV2.Listable[domainsV2.RRSet], error) { | ||
args := client.Called(ctx, zoneID, opts) | ||
rrsets := args.Get(0).(domainsV2.Listable[domainsV2.RRSet]) | ||
err := args.Error(1) | ||
|
||
return rrsets, err | ||
} | ||
|
||
func TestGetZoneByName_whenNeededZoneInResponseWithOffset(t *testing.T) { | ||
nameForSearch := "test.xyz." | ||
correctIDForSearch := "mocked-uuid-2" | ||
|
||
mDNSClient := new(mockedDNSv2Client) | ||
ctx := context.Background() | ||
nextOffset := 3 | ||
opts1 := &map[string]string{ | ||
"filter": nameForSearch, | ||
"limit": "1000", | ||
"offset": "0", | ||
} | ||
opts2 := &map[string]string{ | ||
"filter": nameForSearch, | ||
"limit": "1000", | ||
"offset": strconv.Itoa(nextOffset), | ||
} | ||
incorrectNameForSearch := "a." + nameForSearch | ||
incorrectIDForSearch := "mocked-uuid-1" | ||
zonesWithNextOffset := domainsV2.Listable[domainsV2.Zone](domainsV2.List[domainsV2.Zone]{ | ||
Count: 1, | ||
NextOffset: nextOffset, | ||
Items: []*domainsV2.Zone{ | ||
{ | ||
ID: incorrectIDForSearch, | ||
Name: incorrectNameForSearch, | ||
}, | ||
}, | ||
}) | ||
mDNSClient.On("ListZones", ctx, opts1).Return(zonesWithNextOffset, nil) | ||
zonesWithoutNextOffset := domainsV2.Listable[domainsV2.Zone](domainsV2.List[domainsV2.Zone]{ | ||
Count: 1, | ||
NextOffset: 0, | ||
Items: []*domainsV2.Zone{ | ||
{ | ||
ID: correctIDForSearch, | ||
Name: nameForSearch, | ||
}, | ||
}, | ||
}) | ||
mDNSClient.On("ListZones", ctx, opts2).Return(zonesWithoutNextOffset, nil) | ||
|
||
zone, err := getZoneByName(ctx, mDNSClient, nameForSearch) | ||
|
||
assert.NoError(t, err) | ||
|
||
assert.NotNil(t, zone) | ||
assert.Equal(t, correctIDForSearch, zone.ID) | ||
assert.Equal(t, nameForSearch, zone.Name) | ||
} | ||
|
||
func TestGetRRSetByNameAndType_whenNeededRRSetInResponseWithOffset(t *testing.T) { | ||
rrsetNameForSearch := "test.xyz." | ||
rrsetTypeForSearch := "A" | ||
correctIDForSearch := "mocked-uuid-2" | ||
mockedZoneID := "mopcked-zone-id" | ||
mDNSClient := new(mockedDNSv2Client) | ||
ctx := context.Background() | ||
nextOffset := 3 | ||
opts1 := &map[string]string{ | ||
"name": rrsetNameForSearch, | ||
"rrset_types": rrsetTypeForSearch, | ||
"limit": "1000", | ||
"offset": "0", | ||
} | ||
opts2 := &map[string]string{ | ||
"name": rrsetNameForSearch, | ||
"rrset_types": rrsetTypeForSearch, | ||
"limit": "1000", | ||
"offset": strconv.Itoa(nextOffset), | ||
} | ||
incorrectNameForSearch := "a." + rrsetNameForSearch | ||
incorrectIDForSearch := "mocked-uuid-1" | ||
rrsetWithNextOffset := domainsV2.Listable[domainsV2.RRSet](domainsV2.List[domainsV2.RRSet]{ | ||
Count: 1, | ||
NextOffset: nextOffset, | ||
Items: []*domainsV2.RRSet{ | ||
{ | ||
ID: incorrectIDForSearch, | ||
Name: incorrectNameForSearch, | ||
Type: domainsV2.RecordType(rrsetTypeForSearch), | ||
}, | ||
}, | ||
}) | ||
mDNSClient.On("ListRRSets", ctx, mockedZoneID, opts1).Return(rrsetWithNextOffset, nil) | ||
rrsetsWithoutNextOffset := domainsV2.Listable[domainsV2.RRSet](domainsV2.List[domainsV2.RRSet]{ | ||
Count: 1, | ||
NextOffset: 0, | ||
Items: []*domainsV2.RRSet{ | ||
{ | ||
ID: correctIDForSearch, | ||
Name: rrsetNameForSearch, | ||
Type: domainsV2.RecordType(rrsetTypeForSearch), | ||
}, | ||
}, | ||
}) | ||
mDNSClient.On("ListRRSets", ctx, mockedZoneID, opts2).Return(rrsetsWithoutNextOffset, nil) | ||
|
||
rrset, err := getRRSetByNameAndType(ctx, mDNSClient, mockedZoneID, rrsetNameForSearch, rrsetTypeForSearch) | ||
|
||
assert.NoError(t, err) | ||
|
||
assert.NotNil(t, rrset) | ||
assert.Equal(t, correctIDForSearch, rrset.ID) | ||
assert.Equal(t, rrsetNameForSearch, rrset.Name) | ||
assert.Equal(t, rrsetTypeForSearch, string(rrset.Type)) | ||
} |
Oops, something went wrong.