Skip to content
This repository has been archived by the owner on Nov 12, 2023. It is now read-only.

Commit

Permalink
implement datasource for zone and resource and datasource for record
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Richter <[email protected]>
  • Loading branch information
danielr1996 committed Apr 26, 2021
1 parent 1209b7e commit 5ef58cd
Show file tree
Hide file tree
Showing 9 changed files with 260 additions and 718 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ dist
.terraform.lock.hcl
terraform.tf
Taskfile.yml
crash.log
terraform.tfstate
terraform.tfstate.backup
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module github.com/danielr1996/terraform-provider-hdns

go 1.15
go 1.16

require (
github.com/agext/levenshtein v1.2.3 // indirect
github.com/danielr1996/hdns-go v0.0.1
github.com/danielr1996/hdns-go v1.0.0
github.com/fatih/color v1.10.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-hclog v0.16.0 // indirect
Expand Down
587 changes: 0 additions & 587 deletions go.sum

This file was deleted.

117 changes: 0 additions & 117 deletions hdns/terraform/data_source_zones.go

This file was deleted.

4 changes: 2 additions & 2 deletions main.go
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()
},
})
}
19 changes: 9 additions & 10 deletions hdns/terraform/provider.go → terraform/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package terraform
import (
"context"
"errors"
"github.com/danielr1996/hdns-go/src/hdns"
"github.com/danielr1996/hdns-go/client"
"github.com/danielr1996/terraform-provider-hdns/terraform/record"
"github.com/danielr1996/terraform-provider-hdns/terraform/zone"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

// Provider -
func Provider() *schema.Provider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
Expand All @@ -19,23 +20,21 @@ func Provider() *schema.Provider {
},
},
ConfigureContextFunc: providerConfigure,
ResourcesMap: map[string]*schema.Resource{},
ResourcesMap: map[string]*schema.Resource{
"hdns_record": record.Resource(),
},
DataSourcesMap: map[string]*schema.Resource{
"hdns_zones": dataSourceZones(),
"hdns_zone": zone.DataSource(),
"hdns_record": record.DataSource(),
},
}
}

func providerConfigure(ctx context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) {
token := d.Get("token").(string)

// Warning or errors can be collected in a slice type
var diags diag.Diagnostics

if token == ""{
return nil, diag.FromErr(errors.New("token must not be empty"))
}

client := hdns.NewClient().WithToken(token)
return client, diags
return client.New().WithToken(token), diags
}
86 changes: 86 additions & 0 deletions terraform/record/datasource.go
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
}
104 changes: 104 additions & 0 deletions terraform/record/resource.go
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
}
Loading

0 comments on commit 5ef58cd

Please sign in to comment.