Skip to content

Commit

Permalink
New skyinfoblox api
Browse files Browse the repository at this point in the history
  • Loading branch information
grubert65 authored and ruimoreira committed Sep 21, 2017
1 parent ca204f0 commit b4b1e77
Show file tree
Hide file tree
Showing 144 changed files with 3,941 additions and 8,287 deletions.
4 changes: 2 additions & 2 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ test: fmtcheck
xargs -t -n4 go test $(TESTARGS) -timeout=30s -parallel=4

testacc: fmtcheck
TF_ACC=1 go test $(TEST) -v $(TESTARGS) -timeout 120m
TF_ACC=1 go test $(TEST) -v $(TESTARGS)

testrace: fmtcheck
TF_ACC= go test -race $(TEST) $(TESTARGS)
Expand Down Expand Up @@ -55,4 +55,4 @@ test-compile: fmtcheck
fi
go test -c $(TEST) $(TESTARGS)

.PHONY: build test testacc testrace cover vet fmt fmtcheck errcheck vendor-status test-compile
.PHONY: build test testacc testrace cover vet fmt fmtcheck errcheck vendor-status test-compile
22 changes: 0 additions & 22 deletions infoblox/config.go

This file was deleted.

21 changes: 21 additions & 0 deletions infoblox/getClient.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package infoblox

import (
"github.com/sky-uk/skyinfoblox"
"os"
)

//GetClient - get a skyinfoblox client..
func GetClient() *skyinfoblox.Client {
params := skyinfoblox.Params{
WapiVersion: "v2.6.1", // this is anyhow the default...
URL: os.Getenv("INFOBLOX_SERVER"),
User: os.Getenv("INFOBLOX_USERNAME"),
Password: os.Getenv("INFOBLOX_PASSWORD"),
IgnoreSSL: true,
Debug: true,
}
client := skyinfoblox.Connect(params)

return client
}
38 changes: 31 additions & 7 deletions infoblox/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
"github.com/sky-uk/skyinfoblox"
"time"
)

// Provider : The infoblox terraform provider
Expand Down Expand Up @@ -35,6 +36,18 @@ func Provider() terraform.ResourceProvider {
DefaultFunc: schema.EnvDefaultFunc("INFOBLOX_ALLOW_UNVERIFIED_SSL", false),
Description: "If set, Infoblox client will permit unverifiable SSL certificates.",
},
"wapi_version": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "Infoblox WAPI server version, defaults to v2.6.1",
Default: "v2.6.1",
},
"timeout": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Description: "http response timeout, in seconds",
DefaultFunc: schema.EnvDefaultFunc("INFOBLOX_CLIENT_TIMEOUT", 0),
},
"client_debug": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Expand Down Expand Up @@ -69,15 +82,26 @@ func Provider() terraform.ResourceProvider {
ConfigureFunc: providerConfigure,
}
}

func providerConfigure(d *schema.ResourceData) (interface{}, error) {

username := d.Get("username").(string)
password := d.Get("password").(string)
server := d.Get("server").(string)
ignoreSSL := d.Get("allow_unverified_ssl").(bool)
clientDebug := d.Get("client_debug").(bool)
var seconds int64
seconds = int64(d.Get("timeout").(int))

params := skyinfoblox.Params{
WapiVersion: d.Get("wapi_version").(string),
URL: d.Get("server").(string),
User: d.Get("username").(string),
Password: d.Get("password").(string),
IgnoreSSL: d.Get("allow_unverified_ssl").(bool),
Debug: d.Get("client_debug").(bool),
Timeout: time.Duration(seconds),
}

client := skyinfoblox.Connect(params)

ibxClient := skyinfoblox.NewInfobloxClient(server, username, password, ignoreSSL, clientDebug)
outParams := make(map[string]interface{})
outParams["ibxClient"] = client

return ibxClient, nil
return outParams, nil
}
2 changes: 2 additions & 0 deletions infoblox/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,6 @@ func testAccPreCheck(t *testing.T) {
if v := os.Getenv("INFOBLOX_SERVER"); v == "" {
t.Fatal("INFOBLOX_SERVER must be set for acceptance tests")
}

os.Setenv("INFOBLOX_CLIENT_TIMEOUT", "60")
}
208 changes: 208 additions & 0 deletions infoblox/resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
package infoblox

import (
"github.com/hashicorp/terraform/helper/schema"
"github.com/sky-uk/skyinfoblox"
"log"
"reflect"
)

// CreateResource - Creates a new resource provided its resource schema
func CreateResource(name string, resource *schema.Resource, d *schema.ResourceData, m interface{}) error {

obj := make(map[string]interface{})
attrs := GetAttrs(resource)
for _, attr := range attrs {
key := attr.Name
log.Println("Found attribute: ", key)
if v, ok := d.GetOk(key); ok {
attr.Value = v
obj[key] = GetValue(attr)
}
}

params := m.(map[string]interface{})
client := params["ibxClient"].(*skyinfoblox.Client)

log.Printf("Going to create an %s object: %+v", name, obj)
ref, err := client.Create(name, obj)
if err != nil {
d.SetId("")
return err
}
d.SetId(ref)
return ReadResource(resource, d, m)
}

// CreateAndReadResource - Creates a new resource provided its resource schema and read it back
func CreateAndReadResource(name string, resource *schema.Resource, d *schema.ResourceData, m interface{}) error {

obj := make(map[string]interface{})
attrs := GetAttrs(resource)
for _, attr := range attrs {
key := attr.Name
log.Println("Found attribute: ", key)
if v, ok := d.GetOk(key); ok {
attr.Value = v
obj[key] = GetValue(attr)
}
}

params := m.(map[string]interface{})
client := params["ibxClient"].(*skyinfoblox.Client)

log.Printf("Going to create an %s object: %+v", name, obj)
createdObj, err := client.CreateAndRead(name, obj)
if err != nil {
d.SetId("")
return err
}

d.SetId(createdObj["_ref"].(string))
delete(createdObj, "_ref")
for key := range createdObj {
if isScalar(createdObj[key]) == true {
log.Printf("Setting key %s to %+v\n", key, createdObj[key])
d.Set(key, createdObj[key])
}
}
return nil
}
func isScalar(field interface{}) bool {
t := reflect.TypeOf(field)
if t == nil {
return false
}
k := t.Kind()
switch k {
case reflect.Slice:
return false
case reflect.Map:
return false
}
return true
}

// ReadResource - Reads a resource provided its resource schema
func ReadResource(resource *schema.Resource, d *schema.ResourceData, m interface{}) error {

params := m.(map[string]interface{})
client := params["ibxClient"].(*skyinfoblox.Client)

ref := d.Id()
obj := make(map[string]interface{})

attrs := GetAttrs(resource)
keys := []string{}
for _, attr := range attrs {
keys = append(keys, attr.Name)
}
err := client.Read(ref, keys, &obj)
if err != nil {
d.SetId("")
return err
}

delete(obj, "_ref")
for key := range obj {
if isScalar(obj[key]) == true {
log.Printf("Setting key %s to %+v\n", key, obj[key])
d.Set(key, obj[key])
}
}

return nil
}

// DeleteResource - Deletes a resource
func DeleteResource(d *schema.ResourceData, m interface{}) error {

params := m.(map[string]interface{})
client := params["ibxClient"].(*skyinfoblox.Client)

ref := d.Id()
ref, err := client.Delete(ref)
if err != nil {
return err
}

d.SetId("")
return nil
}

// UpdateResource - Updates a resource provided its schema
func UpdateResource(resource *schema.Resource, d *schema.ResourceData, m interface{}) error {

needsUpdate := false

params := m.(map[string]interface{})
client := params["ibxClient"].(*skyinfoblox.Client)

ref := d.Id()
obj := make(map[string]interface{})

attrs := GetAttrs(resource)
for _, attr := range attrs {
key := attr.Name
if d.HasChange(key) {
attr.Value = d.Get(key)
obj[key] = GetValue(attr)
log.Printf("Updating field %s, value: %+v\n", key, obj[key])
needsUpdate = true
}
}

log.Printf("UPDATE: going to update reference %s with obj: \n%+v\n", ref, obj)

if needsUpdate {
newRef, err := client.Update(ref, obj)
if err != nil {
return err
}
d.SetId(newRef)
}

return ReadResource(resource, d, m)
}

// UpdateAndReadResource - Updates a resource provided its schema
func UpdateAndReadResource(resource *schema.Resource, d *schema.ResourceData, m interface{}) error {

needsUpdate := false

params := m.(map[string]interface{})
client := params["ibxClient"].(*skyinfoblox.Client)

ref := d.Id()
obj := make(map[string]interface{})

attrs := GetAttrs(resource)
for _, attr := range attrs {
key := attr.Name
if d.HasChange(key) {
attr.Value = d.Get(key)
obj[key] = GetValue(attr)
log.Printf("Updating field %s, value: %+v\n", key, obj[key])
needsUpdate = true
}
}

log.Printf("UPDATE: going to update reference %s with obj: \n%+v\n", ref, obj)

if needsUpdate {
newObject, err := client.UpdateAndRead(ref, obj)
if err != nil {
return err
}
d.SetId(newObject["_ref"].(string))
delete(newObject, "_ref")
for key := range newObject {
if isScalar(newObject[key]) == true {
log.Printf("Updating key %s to %+v\n", key, newObject[key])
d.Set(key, newObject[key])
}
}
}

return nil
}
Loading

0 comments on commit b4b1e77

Please sign in to comment.