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

Added entities for work with DNS v2 API. #249

Merged
merged 18 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ require (
github.com/hashicorp/terraform-plugin-sdk/v2 v2.24.1
github.com/selectel/craas-go v0.3.0
github.com/selectel/dbaas-go v0.9.0
github.com/selectel/domains-go v0.5.0
github.com/selectel/domains-go v1.0.2
github.com/selectel/go-selvpcclient/v3 v3.1.1
github.com/selectel/mks-go v0.12.0
github.com/stretchr/testify v1.7.2
github.com/stretchr/testify v1.8.4
)

require (
Expand Down Expand Up @@ -51,6 +51,7 @@ require (
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/oklog/run v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect
github.com/vmihailenco/msgpack/v4 v4.3.12 // indirect
github.com/vmihailenco/tagparser v0.1.1 // indirect
Expand Down
16 changes: 12 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d h1:kJCB4vdITiW1eC1
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM=
github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU=
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
github.com/jarcoal/httpmock v1.2.0 h1:gSvTxxFR/MEMfsGrvRbdfpRUMBStovlSRLw0Ep1bwwc=
github.com/jarcoal/httpmock v1.3.1 h1:iUx3whfZWVf3jT01hQTO/Eo5sAYtB2/rqaUuOtpInww=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4=
Expand Down Expand Up @@ -153,8 +153,10 @@ github.com/selectel/craas-go v0.3.0 h1:tXiw3LNN+ZVV0wZdeBBXX6u8kMuA5PV/5W1uYqV0y
github.com/selectel/craas-go v0.3.0/go.mod h1:9RAUn9PdMITP4I3GAade6v2hjB2j3lo3J2dDlG5SLYE=
github.com/selectel/dbaas-go v0.9.0 h1:IAmiyxkRtfLZg1JUdIhcsE5jpdBvsZibPCqyhB+yV30=
github.com/selectel/dbaas-go v0.9.0/go.mod h1:8D945oFzpx94v08zIb4s1bRTPCdPoNVnBu4umMYFJrQ=
github.com/selectel/domains-go v0.5.0 h1:RCrWY/9KHVtfdA+X8M+DDzsjILxFChhY70HnJEu1Y2U=
github.com/selectel/domains-go v0.5.0/go.mod h1:AhXhwyMSTkpEWFiBLUvzFP76W+WN+ZblwmjLJLt7y58=
github.com/selectel/domains-go v1.0.1 h1:Dau3uVlAXA3iFpeUQq2ld/aOlc8bzwdAXdVPGMwCD7Q=
github.com/selectel/domains-go v1.0.1/go.mod h1:SugRKfq4sTpnOHquslCpzda72wV8u0cMBHx0C0l+bzA=
github.com/selectel/domains-go v1.0.2 h1:Si6iGaMnTFJxwiJVI50DOdZnwcxc87kqaWrVQYW0a4U=
github.com/selectel/domains-go v1.0.2/go.mod h1:SugRKfq4sTpnOHquslCpzda72wV8u0cMBHx0C0l+bzA=
github.com/selectel/go-selvpcclient/v3 v3.1.1 h1:C1q2LqqosiapoLpnGITGmysg0YCSQYDo2Gh69CioevM=
github.com/selectel/go-selvpcclient/v3 v3.1.1/go.mod h1:NM7IXhh1IzqZ88DOw1Qc5Ez3tULLViXo95l5+rKPuyQ=
github.com/selectel/mks-go v0.12.0 h1:nLWHK8BXkhFlXvjFqf7WRrdAfvmrOhQzDSLx7BGa6aM=
Expand All @@ -164,12 +166,18 @@ github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8s=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
github.com/vmihailenco/msgpack v4.0.4+incompatible h1:dSLoQfGFAo3F6OoNhwUmLwVgaUXK79GlxNBwueZn0xI=
github.com/vmihailenco/msgpack v4.0.4+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
Expand Down
94 changes: 94 additions & 0 deletions selectel/data_source_selectel_domains_rrset_v2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package selectel

import (
"context"
"errors"
"fmt"
"log"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

var (
ErrRrsetNotFound = errors.New("rrset not found")
ErrFoundMultipleRRsets = errors.New("found multiple rrsets")
dchudik marked this conversation as resolved.
Show resolved Hide resolved
)

func dataSourceDomainsRrsetV2() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceDomainsRrsetV2Read,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"type": {
Type: schema.TypeString,
Required: true,
},
"zone_id": {
Type: schema.TypeString,
Required: true,
},
"project_id": {
Type: schema.TypeString,
Optional: true,
},
"comment": {
Type: schema.TypeString,
Computed: true,
},
"managed_by": {
Type: schema.TypeString,
Computed: true,
},
"ttl": {
Type: schema.TypeInt,
Computed: true,
},
"records": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"content": {
Type: schema.TypeString,
Computed: true,
},
"disabled": {
Type: schema.TypeBool,
Computed: true,
},
},
},
},
},
}
}

func dataSourceDomainsRrsetV2Read(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client, err := getDomainsV2Client(d, meta)
if err != nil {
return diag.FromErr(err)
}

rrsetName := d.Get("name").(string)
zoneID := d.Get("zone_id").(string)
rrsetType := d.Get("type").(string)

zoneIDWithRrsetNameAndType := fmt.Sprintf("zone_id: %s, rrset_name: %s, rrset_type: %s", zoneID, rrsetName, rrsetType)
log.Println(msgGet(objectRrset, zoneIDWithRrsetNameAndType))

rrset, err := getRrsetByNameAndType(ctx, client, zoneID, rrsetName, rrsetType)
if err != nil {
return diag.FromErr(err)
}

err = setRrsetToResourceData(d, rrset)
if err != nil {
return diag.FromErr(err)
}

return nil
}
68 changes: 68 additions & 0 deletions selectel/data_source_selectel_domains_rrset_v2_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package selectel

import (
"errors"
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
domainsV2 "github.com/selectel/domains-go/pkg/v2"
)

const resourceRrsetName = "rrset_tf_acc_test_1"

func TestAccDomainsRrsetV2DataSourceBasic(t *testing.T) {
testZoneName := fmt.Sprintf("%s.ru.", acctest.RandomWithPrefix("tf-acc"))
testRrsetName := fmt.Sprintf("%[1]s.%[2]s", acctest.RandomWithPrefix("tf-acc"), testZoneName)
testRrsetType := domainsV2.TXT
testRrsetTTL := 60
testRrrsetContent := fmt.Sprintf("\"%[1]s\"", acctest.RandString(16))
dataSourceRrrsetName := fmt.Sprintf("data.selectel_domains_rrset_v2.%[1]s", resourceRrsetName)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccSelectelPreCheckWithProjectID(t) },
ProviderFactories: testAccProviders,
CheckDestroy: testAccCheckDomainsV2RrsetDestroy,
Steps: []resource.TestStep{
{
Config: testAccDomainsRrsetV2DataSourceBasic(resourceRrsetName, testRrsetName, string(testRrsetType), testRrrsetContent, testRrsetTTL, resourceZoneName, testZoneName),
Check: resource.ComposeTestCheckFunc(
testAccDomainsRrsetV2ID(dataSourceRrrsetName),
resource.TestCheckResourceAttr(dataSourceRrrsetName, "name", testRrsetName),
resource.TestCheckResourceAttr(dataSourceRrrsetName, "type", string(testRrsetType)),
resource.TestCheckResourceAttrSet(dataSourceRrrsetName, "zone_id"),
),
},
},
})
}

func testAccDomainsRrsetV2ID(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("can't find rrset: %s", name)
}

if rs.Primary.ID == "" {
return errors.New("rrset data source ID not set")
}

return nil
}
}

func testAccDomainsRrsetV2DataSourceBasic(resourceRrsetName, rrsetName, rrsetType, rrsetContent string, ttl int, resourceZoneName, zoneName string) string {
return fmt.Sprintf(`
%[1]s

%[2]s

data "selectel_domains_rrset_v2" %[3]q {
name = selectel_domains_rrset_v2.%[3]s.name
type = selectel_domains_rrset_v2.%[3]s.type
zone_id = selectel_domains_zone_v2.%[4]s.id
}
`, testAccDomainsZoneV2Basic(resourceZoneName, zoneName), testAccDomainsRrsetV2Basic(resourceRrsetName, rrsetName, rrsetType, rrsetContent, ttl, resourceZoneName), resourceRrsetName, resourceZoneName)
}
82 changes: 82 additions & 0 deletions selectel/data_source_selectel_domains_zone_v2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package selectel

import (
"context"
"errors"
"log"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

var (
ErrZoneNotFound = errors.New("zone not found")
ErrFoundMultipleZones = errors.New("found multiple zones")
)

func dataSourceDomainsZoneV2() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceDomainsZoneV2Read,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"project_id": {
Type: schema.TypeString,
Optional: true,
},
"comment": {
Type: schema.TypeString,
Computed: true,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
},
"updated_at": {
Type: schema.TypeString,
Computed: true,
},
"delegation_checked_at": {
Type: schema.TypeString,
Computed: true,
},
"last_check_status": {
Type: schema.TypeBool,
Computed: true,
},
"last_delegated_at": {
Type: schema.TypeString,
Computed: true,
},
"disabled": {
Type: schema.TypeBool,
Computed: true,
},
},
}
}

func dataSourceDomainsZoneV2Read(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client, err := getDomainsV2Client(d, meta)
if err != nil {
return diag.FromErr(err)
}

zoneName := d.Get("name").(string)

log.Println(msgGet(objectZone, zoneName))

zone, err := getZoneByName(ctx, client, zoneName)
if err != nil {
return diag.FromErr(err)
}

err = setZoneToResourceData(d, zone)
if err != nil {
return diag.FromErr(errGettingObject(objectZone, zoneName, err))
}

return nil
}
67 changes: 67 additions & 0 deletions selectel/data_source_selectel_domains_zone_v2_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package selectel

import (
"context"
"errors"
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

const resourceZoneName = "zone_tf_acc_test_1"

func TestAccDomainsZoneV2DataSourceBasic(t *testing.T) {
testZoneName := fmt.Sprintf("%s.ru.", acctest.RandomWithPrefix("tf-acc"))
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccSelectelPreCheckWithProjectID(t) },
ProviderFactories: testAccProviders,
CheckDestroy: testAccCheckDomainsV2ZoneDestroy,
Steps: []resource.TestStep{
{
Config: testAccDomainsZoneV2DataSourceBasic(resourceZoneName, testZoneName),
Check: resource.ComposeTestCheckFunc(
testAccDomainsZoneV2Exists(fmt.Sprintf("data.selectel_domains_zone_v2.%[1]s", resourceZoneName)),
resource.TestCheckResourceAttr(fmt.Sprintf("data.selectel_domains_zone_v2.%[1]s", resourceZoneName), "name", testZoneName),
),
},
},
})
}

func testAccDomainsZoneV2Exists(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("can't find zone: %s", name)
}

zoneID := rs.Primary.ID
if zoneID == "" {
return errors.New("zone ID not set in tf state")
}
client, err := getDomainsV2ClientTest(rs, testAccProvider)
if err != nil {
return err
}
ctx := context.Background()
_, err = client.GetZone(ctx, zoneID, nil)
if err != nil {
return errors.New("zone in api not found")
}

return nil
}
}

func testAccDomainsZoneV2DataSourceBasic(resourceName, zoneName string) string {
return fmt.Sprintf(`
%[1]s

data "selectel_domains_zone_v2" %[2]q {
name = selectel_domains_zone_v2.%[2]s.name
}
`, testAccDomainsZoneV2Basic(resourceName, zoneName), resourceName, zoneName)
}
Loading