Skip to content

Commit

Permalink
Merge pull request #12 from rfranks-securenet/lockfiles
Browse files Browse the repository at this point in the history
Lockfiles
  • Loading branch information
rayterrill authored Aug 14, 2020
2 parents 2b73cec + d54bb32 commit 901bf19
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 5 deletions.
14 changes: 11 additions & 3 deletions windns/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"github.com/hashicorp/terraform/terraform"

"fmt"
"os"
"io/ioutil"
)

// Provider allows making changes to Windows DNS server
Expand Down Expand Up @@ -71,21 +73,27 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {

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

f, err := ioutil.TempFile("", "terraform-windns")
lockfile := f.Name()
os.Remove(f.Name())

client := DNSClient {
username: username,
password: password,
server: server,
usessl: usessl,
usessh: usessh,
usessh: usessh,
lockfile: lockfile,
}

return &client, nil
return &client, err
}

type DNSClient struct {
username string
password string
server string
usessl string
usessh string
usessh string
lockfile string
}
54 changes: 52 additions & 2 deletions windns/resource_windns.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,36 @@ import (

"errors"
"strings"

"os"
"time"
"math/rand"
)

func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}

func waitForLock(client *DNSClient) bool {

r := rand.New(rand.NewSource(time.Now().UnixNano()))
time.Sleep(time.Duration(r.Intn(100)) * time.Millisecond)

locked := fileExists(client.lockfile)

for locked == true {
time.Sleep(100 * time.Millisecond)
locked = fileExists(client.lockfile)
}

time.Sleep(1000 * time.Millisecond)
return true
}

func resourceWinDNSRecord() *schema.Resource {
return &schema.Resource{
Create: resourceWinDNSRecordCreate,
Expand Down Expand Up @@ -65,6 +93,13 @@ func resourceWinDNSRecordCreate(d *schema.ResourceData, m interface{}) error {

var psCommand string

waitForLock(client)

file, err := os.Create(client.lockfile)
if err != nil {
return err
}

switch record_type {
case "A":
if ipv4address == "" {
Expand All @@ -85,14 +120,18 @@ func resourceWinDNSRecordCreate(d *schema.ResourceData, m interface{}) error {
return errors.New("Unknown record type. This provider currently only supports 'A', 'CNAME', and 'PTR' records.")
}

_, err := goPSRemoting.RunPowershellCommand(client.username, client.password, client.server, psCommand, client.usessl, client.usessh)
_, err = goPSRemoting.RunPowershellCommand(client.username, client.password, client.server, psCommand, client.usessl, client.usessh)

if err != nil {
//something bad happened
return err
}

d.SetId(id)

file.Close()
os.Remove(client.lockfile)

return nil
}

Expand Down Expand Up @@ -128,14 +167,22 @@ func resourceWinDNSRecordDelete(d *schema.ResourceData, m interface{}) error {
//convert the interface so we can use the variables like username, etc
client := m.(*DNSClient)


waitForLock(client)

file, err := os.Create(client.lockfile)
if err != nil {
return err
}

zone_name := d.Get("zone_name").(string)
record_type := d.Get("record_type").(string)
record_name := d.Get("record_name").(string)

//Remove-DnsServerResourceRecord -ZoneName "contoso.com" -RRType "A" -Name "Host01"
var psCommand string = "Remove-DNSServerResourceRecord -ZoneName " + zone_name + " -RRType " + record_type + " -Name " + record_name + " -Confirm:$false -Force"

_, err := goPSRemoting.RunPowershellCommand(client.username, client.password, client.server, psCommand, client.usessl, client.usessh)
_, err = goPSRemoting.RunPowershellCommand(client.username, client.password, client.server, psCommand, client.usessl, client.usessh)
if err != nil {
//something bad happened
return err
Expand All @@ -144,5 +191,8 @@ func resourceWinDNSRecordDelete(d *schema.ResourceData, m interface{}) error {
// d.SetId("") is automatically called assuming delete returns no errors, but it is added here for explicitness.
d.SetId("")

file.Close()
os.Remove(client.lockfile)

return nil
}

0 comments on commit 901bf19

Please sign in to comment.