-
Notifications
You must be signed in to change notification settings - Fork 79
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
Import functionality #225
Changes from 2 commits
47d1b44
c93df1d
5b3fccb
9a890e2
495d969
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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" | ||||||
) | ||||||
|
||||||
|
@@ -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. | ||||||
func newInternalResourceIdFromString(id string) *internalResourceId { | ||||||
newUUID, err := uuid.Parse(id) | ||||||
if err != nil { | ||||||
return nil | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
// - 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) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why don't you return the This will simplify the functions logic down the stack. Each time you're using the Instead of formatting error in the caller function each time, just format it in There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||||||
|
@@ -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{ | ||||||
|
@@ -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{ | ||||||
|
@@ -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 | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import ( | |
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
|
||
ibclient "github.com/infobloxopen/infoblox-go-client/v2" | ||
) | ||
|
||
|
@@ -15,6 +16,10 @@ func resourceARecord() *schema.Resource { | |
Update: resourceARecordUpdate, | ||
Delete: resourceARecordDelete, | ||
|
||
Importer: &schema.ResourceImporter{ | ||
State: stateImporter, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
@@ -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 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.