Skip to content

Commit

Permalink
Merge pull request #646 in CLOUD/terraform-provider-yandex-mirror fro…
Browse files Browse the repository at this point in the history
…m CLOUD-100380/parse-fqdn to master

Squashed commit of the following:

commit 1594dee5db7ea67e739a9695e645b9437da2622f
Author: Konstantin Dudkov <[email protected]>
Date:   Fri Jul 22 15:48:04 2022 +0300

    CLOUD-100380: hostnameDiffSuppressFunc

commit 800328947e3a3ef3ffeacb09614ce8ff6dd117ce
Author: Konstantin Dudkov <[email protected]>
Date:   Fri Jul 22 15:38:52 2022 +0300

    CLOUD-100380: test

commit 6eea78578b474be4c72add347ca9f0d6f0fc946f
Author: Konstantin Dudkov <[email protected]>
Date:   Fri Jul 22 15:26:18 2022 +0300

    CLOUD-100380: fix parse fqdn to get hostname
  • Loading branch information
kdudkov committed Jul 25, 2022
1 parent cc4f222 commit cb92ab9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
27 changes: 19 additions & 8 deletions yandex/resource_yandex_compute_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,10 +368,11 @@ func resourceYandexComputeInstance() *schema.Resource {
},

"hostname": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
DiffSuppressFunc: hostnameDiffSuppressFunc,
},

"metadata": {
Expand Down Expand Up @@ -1284,12 +1285,18 @@ func prepareCreateInstanceRequest(d *schema.ResourceData, meta *Config) (*comput
}

func parseHostnameFromFQDN(fqdn string) (string, error) {
p := strings.Split(fqdn, ".")
if len(p) < 1 {
return "", fmt.Errorf("failed to get instance hostname from its fqdn")
if !strings.Contains(fqdn, ".") {
return fqdn + ".", nil
}
if strings.HasSuffix(fqdn, ".auto.internal") {
return "", nil
}
if strings.HasSuffix(fqdn, ".internal") {
p := strings.Split(fqdn, ".")
return p[0], nil
}

return p[0], nil
return fqdn, nil
}

func wantChangeAddressSpec(old *compute.PrimaryAddressSpec, new *compute.PrimaryAddressSpec) bool {
Expand Down Expand Up @@ -1560,3 +1567,7 @@ func ensureAllowStoppingForUpdate(d *schema.ResourceData, propNames ...string) e
}
return nil
}

func hostnameDiffSuppressFunc(_, oldValue, newValue string, _ *schema.ResourceData) bool {
return strings.TrimRight(oldValue, ".") == strings.TrimRight(newValue, ".")
}
18 changes: 18 additions & 0 deletions yandex/structures_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -747,3 +747,21 @@ func TestFlattenLocalDiskLocalDisks(t *testing.T) {
})
}
}

func TestConvertFQDN(t *testing.T) {
testdata := map[string]string{
"123.auto.internal": "",
"breathtaking.ru-central1.internal": "breathtaking",
"hello.world": "hello.world",
"breathtaking": "breathtaking.",
}

for fqdn, hostname := range testdata {
t.Run("fqdn "+fqdn, func(t *testing.T) {
h, _ := parseHostnameFromFQDN(fqdn)
if h != hostname {
t.Errorf("%s is not equal to %s", h, hostname)
}
})
}
}

0 comments on commit cb92ab9

Please sign in to comment.