Skip to content

Commit

Permalink
Merge pull request #361 from MusicDin/fix/network-zones
Browse files Browse the repository at this point in the history
Remove extension check from network zones
  • Loading branch information
simondeziel authored Oct 27, 2023
2 parents 6b7a9f4 + 547851b commit a4db600
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 69 deletions.
42 changes: 11 additions & 31 deletions lxd/resource_lxd_network_zone.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package lxd

import (
"fmt"
"log"

"github.com/canonical/lxd/shared/api"
Expand Down Expand Up @@ -59,14 +58,6 @@ func resourceLxdNetworkZoneCreate(d *schema.ResourceData, meta interface{}) erro
return err
}

netZoneExt := "projects_networks_zones"
if !server.HasExtension(netZoneExt) {
return fmt.Errorf(
"Network zones cannot be used because LXD server does not support %q extension",
netZoneExt,
)
}

if v, ok := d.GetOk("project"); ok && v != "" {
project := v.(string)
server = server.UseProject(project)
Expand All @@ -77,7 +68,9 @@ func resourceLxdNetworkZoneCreate(d *schema.ResourceData, meta interface{}) erro
config := resourceLxdConfigMap(d.Get("config"))

log.Printf("[DEBUG] Creating network zone %s with config: %#v", name, config)
req := api.NetworkZonesPost{Name: name}

req := api.NetworkZonesPost{}
req.Name = name
req.Config = config
req.Description = desc

Expand All @@ -101,14 +94,6 @@ func resourceLxdNetworkZoneRead(d *schema.ResourceData, meta interface{}) error
return err
}

netZoneExt := "projects_networks_zones"
if !server.HasExtension(netZoneExt) {
return fmt.Errorf(
"Network zones cannot be used because LXD server does not support %q extension",
netZoneExt,
)
}

if v, ok := d.GetOk("project"); ok && v != "" {
project := v.(string)
server = server.UseProject(project)
Expand Down Expand Up @@ -147,6 +132,7 @@ func resourceLxdNetworkZoneUpdate(d *schema.ResourceData, meta interface{}) erro
}

name := d.Id()

_, etag, err := server.GetNetworkZone(name)
if err != nil {
return err
Expand All @@ -155,10 +141,9 @@ func resourceLxdNetworkZoneUpdate(d *schema.ResourceData, meta interface{}) erro
config := resourceLxdConfigMap(d.Get("config"))
desc := d.Get("description").(string)

req := api.NetworkZonePut{
Config: config,
Description: desc,
}
req := api.NetworkZonePut{}
req.Config = config
req.Description = desc

err = server.UpdateNetworkZone(name, req, etag)
if err != nil {
Expand Down Expand Up @@ -190,10 +175,7 @@ func resourceLxdNetworkZoneDelete(d *schema.ResourceData, meta interface{}) (err
return err
}

func resourceLxdNetworkZoneExists(
d *schema.ResourceData,
meta interface{},
) (exists bool, err error) {
func resourceLxdNetworkZoneExists(d *schema.ResourceData, meta interface{}) (exists bool, err error) {
p := meta.(*lxdProvider)
server, err := p.GetInstanceServer(p.selectRemote(d))
if err != nil {
Expand All @@ -213,20 +195,17 @@ func resourceLxdNetworkZoneExists(
if err != nil && isNotFoundError(err) {
err = nil
}

if err == nil && v != nil {
exists = true
}

return
}

func resourceLxdNetworkZoneImport(
d *schema.ResourceData,
meta interface{},
) ([]*schema.ResourceData, error) {
func resourceLxdNetworkZoneImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
p := meta.(*lxdProvider)
remote, name, err := p.LXDConfig.ParseRemote(d.Id())

if err != nil {
return nil, err
}
Expand All @@ -241,6 +220,7 @@ func resourceLxdNetworkZoneImport(
if err != nil {
return nil, err
}

if v, ok := d.GetOk("project"); ok && v != "" {
project := v.(string)
server = server.UseProject(project)
Expand Down
19 changes: 8 additions & 11 deletions lxd/resource_lxd_network_zone_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ func resourceLxdNetworkZoneRecordCreate(d *schema.ResourceData, meta interface{}
config := resourceLxdConfigMap(d.Get("config"))

log.Printf("[DEBUG] Creating network zone record %s with config: %#v", name, config)
req := api.NetworkZoneRecordsPost{Name: name}

req := api.NetworkZoneRecordsPost{}
req.Name = name
req.Config = config
req.Entries = entries
req.Description = desc
Expand Down Expand Up @@ -218,10 +220,7 @@ func resourceLxdNetworkZoneRecordDelete(d *schema.ResourceData, meta interface{}
return err
}

func resourceLxdNetworkZoneRecordExists(
d *schema.ResourceData,
meta interface{},
) (exists bool, err error) {
func resourceLxdNetworkZoneRecordExists(d *schema.ResourceData, meta interface{}) (exists bool, err error) {
p := meta.(*lxdProvider)
server, err := p.GetInstanceServer(p.selectRemote(d))
if err != nil {
Expand Down Expand Up @@ -249,19 +248,14 @@ func resourceLxdNetworkZoneRecordExists(
return
}

func resourceLxdNetworkZoneRecordImport(
d *schema.ResourceData,
meta interface{},
) ([]*schema.ResourceData, error) {
func resourceLxdNetworkZoneRecordImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
p := meta.(*lxdProvider)
remote, name, err := p.LXDConfig.ParseRemote(d.Id())

if err != nil {
return nil, err
}

d.SetId(name)
zone := d.Get("zone").(string)

if p.LXDConfig.DefaultRemote != remote {
d.Set("remote", remote)
Expand All @@ -271,11 +265,14 @@ func resourceLxdNetworkZoneRecordImport(
if err != nil {
return nil, err
}

if v, ok := d.GetOk("project"); ok && v != "" {
project := v.(string)
server = server.UseProject(project)
}

zone := d.Get("zone").(string)

record, _, err := server.GetNetworkZoneRecord(zone, name)
if err != nil {
return nil, err
Expand Down
6 changes: 1 addition & 5 deletions lxd/resource_lxd_network_zone_record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,7 @@ func TestAccNetworkZoneRecord_zone(t *testing.T) {
})
}

func testAccNetworkZoneRecordExists(
t *testing.T,
n string,
record *api.NetworkZoneRecord,
) resource.TestCheckFunc {
func testAccNetworkZoneRecordExists(t *testing.T, n string, record *api.NetworkZoneRecord) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
Expand Down
35 changes: 13 additions & 22 deletions lxd/resource_lxd_network_zone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,7 @@ func TestAccNetworkZone_project(t *testing.T) {
})
}

func testAccNetworkZoneExists(
t *testing.T,
n string,
zone *api.NetworkZone,
) resource.TestCheckFunc {
func testAccNetworkZoneExists(t *testing.T, n string, zone *api.NetworkZone) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
Expand All @@ -99,6 +95,7 @@ func testAccNetworkZoneExists(
if err != nil {
return err
}

z, _, err := client.GetNetworkZone(rs.Primary.ID)
if err != nil {
return err
Expand All @@ -110,12 +107,7 @@ func testAccNetworkZoneExists(
}
}

func testAccNetworkZoneExistsInProject(
t *testing.T,
n string,
zone *api.NetworkZone,
project string,
) resource.TestCheckFunc {
func testAccNetworkZoneExistsInProject(t *testing.T, n string, zone *api.NetworkZone, project string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
Expand All @@ -130,7 +122,9 @@ func testAccNetworkZoneExistsInProject(
if err != nil {
return err
}

client = client.UseProject(project)

z, _, err := client.GetNetworkZone(rs.Primary.ID)
if err != nil {
return err
Expand All @@ -142,25 +136,22 @@ func testAccNetworkZoneExistsInProject(
}
}

func testAccNetworkZoneConfig(zone *api.NetworkZone, k, v string) resource.TestCheckFunc {
func testAccNetworkZoneConfig(zone *api.NetworkZone, key, value string) resource.TestCheckFunc {
return func(s *terraform.State) error {
if zone.Config == nil {
return fmt.Errorf("No config")
}

for key, value := range zone.Config {
if k != key {
continue
}

if v == value {
return nil
}
v, ok := zone.Config[key]
if !ok {
return fmt.Errorf("Config not found: %s", key)
}

return fmt.Errorf("Bad value for %s: %s", k, value)
if v != value {
return fmt.Errorf("Bad value for %s: %s", key, v)
}

return fmt.Errorf("Config not found: %s", k)
return nil
}
}

Expand Down

0 comments on commit a4db600

Please sign in to comment.