This repository has been archived by the owner on Nov 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement datasource for zone and resource and datasource for record
Signed-off-by: Daniel Richter <[email protected]>
- Loading branch information
1 parent
1209b7e
commit 5ef58cd
Showing
9 changed files
with
260 additions
and
718 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,6 @@ dist | |
.terraform.lock.hcl | ||
terraform.tf | ||
Taskfile.yml | ||
crash.log | ||
terraform.tfstate | ||
terraform.tfstate.backup |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/danielr1996/terraform-provider-hdns/hdns/terraform" | ||
terraform2 "github.com/danielr1996/terraform-provider-hdns/terraform" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/plugin" | ||
) | ||
|
||
func main() { | ||
plugin.Serve(&plugin.ServeOpts{ | ||
ProviderFunc: func() *schema.Provider { | ||
return terraform.Provider() | ||
return terraform2.Provider() | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package record | ||
|
||
import ( | ||
"context" | ||
"github.com/danielr1996/hdns-go/client" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func DataSource() *schema.Resource { | ||
return &schema.Resource{ | ||
ReadContext: dataSourceRecordRead, | ||
Schema: map[string]*schema.Schema{ | ||
"id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Optional: true, | ||
}, | ||
"type": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Optional: true, | ||
}, | ||
"name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Optional: true, | ||
}, | ||
"value": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Optional: true, | ||
}, | ||
"zone_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Optional: true, | ||
}, | ||
"created": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Optional: true, | ||
}, | ||
"modified": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Optional: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceRecordRead(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
var diags diag.Diagnostics | ||
c := m.(*client.Client) | ||
|
||
id, ok := d.GetOk("id") | ||
if !ok{ | ||
return diag.Errorf("Please provide a id argument") | ||
} | ||
record, err := c.Record.GetById(id.(string)) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
d.SetId(record.Id) | ||
if err := d.Set("type", record.Type); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
if err := d.Set("name", record.Name); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
if err := d.Set("value", record.Value); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
if err := d.Set("zone_id", record.ZoneId); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
if err := d.Set("created", record.Created); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
if err := d.Set("modified", record.Modified); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
return diags | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package record | ||
|
||
|
||
import ( | ||
"context" | ||
"github.com/danielr1996/hdns-go/client" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func Resource() *schema.Resource { | ||
return &schema.Resource{ | ||
CreateContext: Create, | ||
ReadContext: Read, | ||
UpdateContext: Update, | ||
DeleteContext: Delete, | ||
Schema: map[string]*schema.Schema{ | ||
"type": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"value": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"zone_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
}, | ||
Importer: &schema.ResourceImporter{ | ||
StateContext: schema.ImportStatePassthroughContext, | ||
}, | ||
} | ||
} | ||
|
||
func Create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
var diags diag.Diagnostics | ||
c := m.(*client.Client) | ||
recordType := d.Get("type").(string) | ||
value := d.Get("value").(string) | ||
name := d.Get("name").(string) | ||
zoneId := d.Get("zone_id").(string) | ||
record, err := c.Record.Create(name, recordType,value,zoneId) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
d.SetId(record.Id) | ||
Read(ctx, d, m) | ||
return diags | ||
} | ||
|
||
func Read(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
var diags diag.Diagnostics | ||
c := m.(*client.Client) | ||
record, err := c.Record.GetById(d.Id()) | ||
if err != nil{ | ||
return diag.FromErr(err) | ||
} | ||
if err := d.Set("type", record.Type); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
if err := d.Set("name", record.Name); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
if err := d.Set("value", record.Value); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
if err := d.Set("zone_id", record.ZoneId); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
return diags | ||
} | ||
|
||
func Update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
c := m.(*client.Client) | ||
if d.HasChanges("type","name","value","zone_id"){ | ||
recordType := d.Get("type").(string) | ||
value := d.Get("value").(string) | ||
name := d.Get("name").(string) | ||
zoneId := d.Get("zone_id").(string) | ||
_, err := c.Record.Update(name, recordType, value, zoneId, d.Id()) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
} | ||
return Read(ctx, d, m) | ||
} | ||
|
||
func Delete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
var diags diag.Diagnostics | ||
c := m.(*client.Client) | ||
err := c.Record.Delete(d.Id()) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
d.SetId("") | ||
return diags | ||
} |
Oops, something went wrong.