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

Import functionality #225

Merged
merged 5 commits into from
Aug 9, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.15
require (
github.com/google/uuid v1.3.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.4.3
github.com/infobloxopen/infoblox-go-client/v2 v2.1.1
github.com/infobloxopen/infoblox-go-client/v2 v2.1.2-0.20220727224704-88a5ba602dbf
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 // indirect
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect
)
10 changes: 2 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -202,14 +202,8 @@ github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpO
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/imdario/mergo v0.3.9 h1:UauaLniWCFHWd+Jp9oCEkTBj8VO/9DKg3PV3VCNMDIg=
github.com/imdario/mergo v0.3.9/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
github.com/infobloxopen/infoblox-go-client/v2 v2.1.1-0.20211119055343-741a1cd7c839 h1:Rf5/dA/cYaWMKltfRK6xssVyJK0bUllK6F5CbsSn1Tk=
github.com/infobloxopen/infoblox-go-client/v2 v2.1.1-0.20211119055343-741a1cd7c839/go.mod h1:+lznx4ASBSUZ2i6qwlgyn0v3eKDxBHNU5aRJzghAFbw=
github.com/infobloxopen/infoblox-go-client/v2 v2.1.1-0.20211213023950-a7b7f78419e2 h1:PRu5l3XfNaHFCtSq8TptwsCNiPp68G6wyECuPXzpFx0=
github.com/infobloxopen/infoblox-go-client/v2 v2.1.1-0.20211213023950-a7b7f78419e2/go.mod h1:+lznx4ASBSUZ2i6qwlgyn0v3eKDxBHNU5aRJzghAFbw=
github.com/infobloxopen/infoblox-go-client/v2 v2.1.1-0.20211223082453-65ce67cf7cce h1:fjRpxdsZ0j5uurjFFb/voYCkp9Qfhe7L7zlRAKCYMG0=
github.com/infobloxopen/infoblox-go-client/v2 v2.1.1-0.20211223082453-65ce67cf7cce/go.mod h1:+lznx4ASBSUZ2i6qwlgyn0v3eKDxBHNU5aRJzghAFbw=
github.com/infobloxopen/infoblox-go-client/v2 v2.1.1 h1:NYnXOngwW4xMIgUz4bKJ1Ygsmq5K0OOdUEkuLlkR1Ag=
github.com/infobloxopen/infoblox-go-client/v2 v2.1.1/go.mod h1:+lznx4ASBSUZ2i6qwlgyn0v3eKDxBHNU5aRJzghAFbw=
github.com/infobloxopen/infoblox-go-client/v2 v2.1.2-0.20220727224704-88a5ba602dbf h1:/4CHlCAQbzdED7T6YZ1ht7uQ8HNBR+eeR5hW44+du54=
github.com/infobloxopen/infoblox-go-client/v2 v2.1.2-0.20220727224704-88a5ba602dbf/go.mod h1:+lznx4ASBSUZ2i6qwlgyn0v3eKDxBHNU5aRJzghAFbw=
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.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
Expand Down
97 changes: 70 additions & 27 deletions infoblox/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (

"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"

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

ibclient "github.com/infobloxopen/infoblox-go-client/v2"
)

Expand All @@ -21,36 +21,74 @@ const (
altIdSeparator = "|"
)

func generateInternalId() string {
return uuid.NewString()
type internalResourceId struct {
value uuid.UUID
}

func generateAltId(internalId string, ref string) string {
return fmt.Sprintf(
"%s%s%s",
internalId, altIdSeparator, ref)
func (id *internalResourceId) Equal(id2 *internalResourceId) bool {
if id2 == nil {
panic("the argument must not be nil")
}
return id.value.String() == id2.value.String()
}

func getAltIdFields(altId string) (
internalId string, ref string, err error) {
func (id *internalResourceId) String() string {
return id.value.String()
}

idParts := strings.SplitN(altId, altIdSeparator, 2)
if len(idParts) != 2 {
err = fmt.Errorf("invalid internal ID for host record: '%s'", altId)
return
// Returns a pointer to parsed internal resource ID, nil otherwise.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Returns a pointer to parsed internal resource ID, nil otherwise.
// newInternalResourceIdFromString returns a pointer to parsed internal resource ID, nil otherwise.

func newInternalResourceIdFromString(id string) *internalResourceId {
newUUID, err := uuid.Parse(id)
if err != nil {
return nil
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we add a log message here, or even panic?

How the user will know if the id is not parsed, and why it's not parsed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure.

}

internalId = idParts[0]
ref = idParts[1]
return
return &internalResourceId{value: newUUID}
}

func renewAltId(oldAltId string, newRef string) (string, error) {
internalId, _, err := getAltIdFields(oldAltId)
func generateInternalId() *internalResourceId {
uuid, err := uuid.NewRandom()
if err != nil {
return "", err
panic(err)
}
return generateAltId(internalId, newRef), nil
return &internalResourceId{value: uuid}
}

func isValidInternalId(internalId string) bool {
_, err := uuid.Parse(internalId)
if err != nil {
return false
}

return true
}

func generateAltId(internalId *internalResourceId, ref string) string {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's AltId? What this function will generate?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This AltId is an ID which is alternative to Terraform resource's ID. Sometimes we cannot use NIOS object's reference as a Terraform resource's ID because the reference may get changed.

if internalId == nil {
panic("the argument must not be nil")
}
return fmt.Sprintf(
"%s%s%s",
internalId.String(), altIdSeparator, ref)
}

// valid = true:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// valid = true:
// valid == true if:

// - exactly 2 parts found
// - ... and separated by the delimiter
// - ... and the 1st one is a valid internal ID
// - ... and the 2nd one is not empty
func getAltIdFields(altId string) (internalId *internalResourceId, ref string, valid bool) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't you return the error value instead of valid bool value?

This will simplify the functions logic down the stack. Each time you're using the valid argument, you format the error in the caller function anyway (here, here, and here).

Instead of formatting error in the caller function each time, just format it in getAltIdFields, and return instead of valid return argument.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably you are right. I do not remember the exact reason. I will try to use the canonical way, and if there will be a real reason why to choose the opposite, I will document it with a comment.

idParts := strings.SplitN(altId, altIdSeparator, 2)
skudriavtsev marked this conversation as resolved.
Show resolved Hide resolved
switch len(idParts) {
case 1:
internalId = newInternalResourceIdFromString(idParts[0])
case 2:
internalId = newInternalResourceIdFromString(idParts[0])
ref = idParts[1]
valid = internalId != nil && ref != ""
}

return
}

func Provider() *schema.Provider {
Expand Down Expand Up @@ -134,9 +172,7 @@ func Provider() *schema.Provider {

}

func providerConfigure(
ctx context.Context,
d *schema.ResourceData) (interface{}, diag.Diagnostics) {
func providerConfigure(ctx context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) {

if d.Get("password") == "" {
return nil, diag.Diagnostics{diag.Diagnostic{
Expand All @@ -147,11 +183,14 @@ func providerConfigure(

seconds := int64(d.Get("connect_timeout").(int))
hostConfig := ibclient.HostConfig{
Host: d.Get("server").(string),
Port: d.Get("port").(string),
Host: d.Get("server").(string),
Port: d.Get("port").(string),
Version: d.Get("wapi_version").(string),
}

authConfig := ibclient.AuthConfig{
Username: d.Get("username").(string),
Password: d.Get("password").(string),
Version: d.Get("wapi_version").(string),
}

transportConfig := ibclient.TransportConfig{
Expand All @@ -163,9 +202,13 @@ func providerConfigure(
requestBuilder := &ibclient.WapiRequestBuilder{}
requestor := &ibclient.WapiHttpRequestor{}

conn, err := ibclient.NewConnector(hostConfig, transportConfig, requestBuilder, requestor)
conn, err := ibclient.NewConnector(hostConfig, authConfig, transportConfig, requestBuilder, requestor)
if err != nil {
return nil, diag.Diagnostics{diag.Diagnostic{Summary: err.Error()}}
}
return conn, nil
}

func stateImporter(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
return []*schema.ResourceData{d}, nil
}
38 changes: 38 additions & 0 deletions infoblox/resource_infoblox_a_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

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

ibclient "github.com/infobloxopen/infoblox-go-client/v2"
)

Expand All @@ -15,6 +16,10 @@ func resourceARecord() *schema.Resource {
Update: resourceARecordUpdate,
Delete: resourceARecordDelete,

Importer: &schema.ResourceImporter{
State: stateImporter,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but I planned to clean up all the deprecated fields/types later. Probably it is possible to do it now easily. I will try.

},

Schema: map[string]*schema.Schema{
"network_view": {
Type: schema.TypeString,
Expand Down Expand Up @@ -145,6 +150,39 @@ func resourceARecordGet(d *schema.ResourceData, m interface{}) error {
return err
}

ttl := int(obj.Ttl)
if !obj.UseTtl {
ttl = ttlUndef
}
if err = d.Set("ttl", ttl); err != nil {
return err
}

if obj.Ea != nil && len(obj.Ea) > 0 {
// TODO: temporary scaffold, need to rework marshalling/unmarshalling of EAs
// (avoiding additional layer of keys ("value" key)
eaMap := (map[string]interface{})(obj.Ea)
ea, err := json.Marshal(eaMap)
if err != nil {
return err
}
if err = d.Set("ext_attrs", string(ea)); err != nil {
return err
}
}

if err = d.Set("comment", obj.Comment); err != nil {
return err
}

if err = d.Set("dns_view", obj.View); err != nil {
return err
}

if err = d.Set("fqdn", obj.Name); err != nil {
return err
}

d.SetId(obj.Ref)

return nil
Expand Down
52 changes: 45 additions & 7 deletions infoblox/resource_infoblox_aaaa_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

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

ibclient "github.com/infobloxopen/infoblox-go-client/v2"
)

Expand All @@ -14,6 +15,9 @@ func resourceAAAARecord() *schema.Resource {
Read: resourceAAAARecordGet,
Update: resourceAAAARecordUpdate,
Delete: resourceAAAARecordDelete,
Importer: &schema.ResourceImporter{
State: stateImporter,
},

Schema: map[string]*schema.Schema{
"network_view": {
Expand Down Expand Up @@ -67,7 +71,6 @@ func resourceAAAARecord() *schema.Resource {
}

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

networkView := d.Get("network_view").(string)
cidr := d.Get("cidr").(string)
ipv6Addr := d.Get("ipv6_addr").(string)
Expand Down Expand Up @@ -125,11 +128,11 @@ func resourceAAAARecordCreate(d *schema.ResourceData, m interface{}) error {
return err
}
d.SetId(recordAAAA.Ref)

return nil
}

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

extAttrJSON := d.Get("ext_attrs").(string)
extAttrs := make(map[string]interface{})
if extAttrJSON != "" {
Expand All @@ -145,19 +148,53 @@ func resourceAAAARecordGet(d *schema.ResourceData, m interface{}) error {
connector := m.(ibclient.IBConnector)
objMgr := ibclient.NewObjectManager(connector, "Terraform", tenantID)

recordAAAA, err := objMgr.GetAAAARecordByRef(d.Id())
obj, err := objMgr.GetAAAARecordByRef(d.Id())
if err != nil {
return fmt.Errorf("getting AAAA Record with ID: %s failed: %s", d.Id(), err.Error())
}
if err = d.Set("ipv6_addr", recordAAAA.Ipv6Addr); err != nil {
if err = d.Set("ipv6_addr", obj.Ipv6Addr); err != nil {
return err
}
d.SetId(recordAAAA.Ref)

ttl := int(obj.Ttl)
if !obj.UseTtl {
ttl = ttlUndef
}
if err = d.Set("ttl", ttl); err != nil {
return err
}

if obj.Ea != nil && len(obj.Ea) > 0 {
// TODO: temporary scaffold, need to rework marshalling/unmarshalling of EAs
// (avoiding additional layer of keys ("value" key)
eaMap := (map[string]interface{})(obj.Ea)
ea, err := json.Marshal(eaMap)
if err != nil {
return err
}
if err = d.Set("ext_attrs", string(ea)); err != nil {
return err
}
}

if err = d.Set("comment", obj.Comment); err != nil {
return err
}

if err = d.Set("dns_view", obj.View); err != nil {
return err
}

if err = d.Set("fqdn", obj.Name); err != nil {
return err
}

d.SetId(obj.Ref)

return nil
}

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

networkView := d.Get("network_view").(string)
if d.HasChange("network_view") {
return fmt.Errorf("changing the value of 'network_view' field is not allowed")
Expand Down Expand Up @@ -233,11 +270,11 @@ func resourceAAAARecordUpdate(d *schema.ResourceData, m interface{}) error {
return err
}
d.SetId(recordAAAA.Ref)

return nil
}

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

dnsView := d.Get("dns_view").(string)

extAttrJSON := d.Get("ext_attrs").(string)
Expand All @@ -261,5 +298,6 @@ func resourceAAAARecordDelete(d *schema.ResourceData, m interface{}) error {
return fmt.Errorf("deletion of AAAA Record from dns view %s failed: %s", dnsView, err.Error())
}
d.SetId("")

return nil
}
Loading