Skip to content

Commit

Permalink
added dog_inventory, auto formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
dgulinobw committed Mar 14, 2023
1 parent 3063655 commit 3b7ebad
Show file tree
Hide file tree
Showing 22 changed files with 756 additions and 236 deletions.
22 changes: 11 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ debug:
dlv exec --accept-multiclient --continue --headless ./bin/terraform-provider-dog -- -debug

release:
#GOOS=darwin GOARCH=amd64 go build -o ./bin/${BINARY}_${VERSION}_darwin_amd64
#GOOS=freebsd GOARCH=386 go build -o ./bin/${BINARY}_${VERSION}_freebsd_386
#GOOS=freebsd GOARCH=amd64 go build -o ./bin/${BINARY}_${VERSION}_freebsd_amd64
#GOOS=freebsd GOARCH=arm go build -o ./bin/${BINARY}_${VERSION}_freebsd_arm
#GOOS=linux GOARCH=386 go build -o ./bin/${BINARY}_${VERSION}_linux_386
GOOS=darwin GOARCH=amd64 go build -o ./bin/${BINARY}_${VERSION}_darwin_amd64
GOOS=freebsd GOARCH=386 go build -o ./bin/${BINARY}_${VERSION}_freebsd_386
GOOS=freebsd GOARCH=amd64 go build -o ./bin/${BINARY}_${VERSION}_freebsd_amd64
GOOS=freebsd GOARCH=arm go build -o ./bin/${BINARY}_${VERSION}_freebsd_arm
GOOS=linux GOARCH=386 go build -o ./bin/${BINARY}_${VERSION}_linux_386
GOOS=linux GOARCH=amd64 go build -o ./bin/${BINARY}_${VERSION}_linux_amd64
#GOOS=linux GOARCH=arm go build -o ./bin/${BINARY}_${VERSION}_linux_arm
#GOOS=openbsd GOARCH=386 go build -o ./bin/${BINARY}_${VERSION}_openbsd_386
#GOOS=openbsd GOARCH=amd64 go build -o ./bin/${BINARY}_${VERSION}_openbsd_amd64
#GOOS=solaris GOARCH=amd64 go build -o ./bin/${BINARY}_${VERSION}_solaris_amd64
#GOOS=windows GOARCH=386 go build -o ./bin/${BINARY}_${VERSION}_windows_386
#GOOS=windows GOARCH=amd64 go build -o ./bin/${BINARY}_${VERSION}_windows_amd64
GOOS=linux GOARCH=arm go build -o ./bin/${BINARY}_${VERSION}_linux_arm
GOOS=openbsd GOARCH=386 go build -o ./bin/${BINARY}_${VERSION}_openbsd_386
GOOS=openbsd GOARCH=amd64 go build -o ./bin/${BINARY}_${VERSION}_openbsd_amd64
GOOS=solaris GOARCH=amd64 go build -o ./bin/${BINARY}_${VERSION}_solaris_amd64
GOOS=windows GOARCH=386 go build -o ./bin/${BINARY}_${VERSION}_windows_386
GOOS=windows GOARCH=amd64 go build -o ./bin/${BINARY}_${VERSION}_windows_amd64

github_release:
goreleaser release --rm-dist
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/hashicorp/terraform-plugin-go v0.14.1
github.com/hashicorp/terraform-plugin-log v0.7.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.24.0
github.com/relaypro-open/dog_api_golang v0.0.0-20230302175531-9d6c47df744f
github.com/relaypro-open/dog_api_golang v0.0.0-20230314165349-3ab62e33385c
golang.org/x/exp v0.0.0-20221114191408-850992195362
)

Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,10 @@ github.com/relaypro-open/dog_api_golang v0.0.0-20230302144827-df2f22eb619a h1:Cn
github.com/relaypro-open/dog_api_golang v0.0.0-20230302144827-df2f22eb619a/go.mod h1:gVISNtT0C/Ej5ZnCePDyppaDQiPRANiq2A/qhx/tH+8=
github.com/relaypro-open/dog_api_golang v0.0.0-20230302175531-9d6c47df744f h1:hWSrRabbh1uOA3cNlJWDfGDQ3Y+oIg4hs9uwCf3FSAQ=
github.com/relaypro-open/dog_api_golang v0.0.0-20230302175531-9d6c47df744f/go.mod h1:gVISNtT0C/Ej5ZnCePDyppaDQiPRANiq2A/qhx/tH+8=
github.com/relaypro-open/dog_api_golang v0.0.0-20230314145046-afc83bae3332 h1:xrCbU0w8FQPu4cX+OrfUaCpi4iRRK8nJ1uEBgvHT9/Q=
github.com/relaypro-open/dog_api_golang v0.0.0-20230314145046-afc83bae3332/go.mod h1:gVISNtT0C/Ej5ZnCePDyppaDQiPRANiq2A/qhx/tH+8=
github.com/relaypro-open/dog_api_golang v0.0.0-20230314165349-3ab62e33385c h1:ErGp9MPmTLJ+Z/onCWkvXwIFbskhRLcl5usX9k7Ctwk=
github.com/relaypro-open/dog_api_golang v0.0.0-20230314165349-3ab62e33385c/go.mod h1:gVISNtT0C/Ej5ZnCePDyppaDQiPRANiq2A/qhx/tH+8=
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
github.com/russross/blackfriday v1.6.0 h1:KqfZb0pUVN2lYqZUYRddxF4OR8ZMURnJIG5Y3VRLtww=
github.com/russross/blackfriday v1.6.0/go.mod h1:ti0ldHuxg49ri4ksnFxlkCfN+hvslNlmVHqNRXXJNAY=
Expand Down
26 changes: 12 additions & 14 deletions internal/provider/datasource_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/datasource"
api "github.com/relaypro-open/dog_api_golang/api"
)

Expand All @@ -19,21 +19,20 @@ type (
GroupList []Group

Group struct {
Description types.String `tfsdk:"description"`
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
ProfileId types.String `tfsdk:"profile_id"`
ProfileName types.String `tfsdk:"profile_name"`
ProfileVersion types.String `tfsdk:"profile_version"`
Description types.String `tfsdk:"description"`
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
ProfileId types.String `tfsdk:"profile_id"`
ProfileName types.String `tfsdk:"profile_name"`
ProfileVersion types.String `tfsdk:"profile_version"`
Ec2SecurityGroupIds []*Ec2SecurityGroupIds `tfsdk:"ec2_security_group_ids"`
Vars map[string]string `tfsdk:"vars"`
Vars map[string]string `tfsdk:"vars"`
}

Ec2SecurityGroupIds struct {
Region types.String `tfsdk:"region"`
SgId types.String `tfsdk:"sgid"`
Region types.String `tfsdk:"region"`
SgId types.String `tfsdk:"sgid"`
}

)

var (
Expand All @@ -44,7 +43,6 @@ func NewGroupDataSource() datasource.DataSource {
return &groupDataSource{}
}


func (*groupDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_group"
}
Expand Down Expand Up @@ -91,7 +89,7 @@ func (d *groupDataSource) Configure(ctx context.Context, req datasource.Configur

type groupDataSourceData struct {
ApiToken types.String `tfsdk:"api_token"`
Id types.String `tfsdk:"id"`
Id types.String `tfsdk:"id"`
}

//type groupDataSource struct {
Expand Down
20 changes: 9 additions & 11 deletions internal/provider/datasource_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/datasource"
api "github.com/relaypro-open/dog_api_golang/api"
)

Expand All @@ -16,17 +16,16 @@ type (
p dogProvider
}


HostList []Host

Host struct {
Environment types.String `tfsdk:"environment"`
Group types.String `tfsdk:"group"`
ID types.String `tfsdk:"id"`
HostKey types.String `tfsdk:"hostkey"`
Location types.String `tfsdk:"location"`
Name types.String `tfsdk:"name"`
Vars map[string]string `tfsdk:"vars"`
Environment types.String `tfsdk:"environment"`
Group types.String `tfsdk:"group"`
ID types.String `tfsdk:"id"`
HostKey types.String `tfsdk:"hostkey"`
Location types.String `tfsdk:"location"`
Name types.String `tfsdk:"name"`
Vars map[string]string `tfsdk:"vars"`
}
)

Expand All @@ -38,7 +37,6 @@ func NewHostDataSource() datasource.DataSource {
return &hostDataSource{}
}


func (*hostDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_host"
}
Expand Down Expand Up @@ -85,7 +83,7 @@ func (d *hostDataSource) Configure(ctx context.Context, req datasource.Configure

type hostDataSourceData struct {
ApiToken types.String `tfsdk:"api_token"`
Id types.String `tfsdk:"id"`
Id types.String `tfsdk:"id"`
}

func (d *hostDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
Expand Down
119 changes: 119 additions & 0 deletions internal/provider/datasource_inventory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package dog

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
api "github.com/relaypro-open/dog_api_golang/api"
)

type (
inventoryDataSource struct {
p dogProvider
}

InventoryList []Inventory

Inventory struct {
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Groups []*InventoryGroup `tfsdk:"groups"`
}

InventoryGroup struct {
Name types.String `tfsdk:"name"`
Vars map[string]string `tfsdk:"vars"`
Hosts map[string]map[string]string `tfsdk:"hosts"`
}
)

var (
_ datasource.DataSource = (*inventoryDataSource)(nil)
)

func NewInventoryDataSource() datasource.DataSource {
return &inventoryDataSource{}
}

func (*inventoryDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_inventory"
}

func (*inventoryDataSource) GetSchema(ctx context.Context) (tfsdk.Schema, diag.Diagnostics) {
return tfsdk.Schema{
// This description is used by the documentation generator and the language server.
MarkdownDescription: "Inventory data source",

Attributes: map[string]tfsdk.Attribute{
"api_token": {
MarkdownDescription: "Inventory configurable attribute",
Optional: true,
Type: types.StringType,
},
"id": {
MarkdownDescription: "Inventory identifier",
Type: types.StringType,
Computed: true,
},
},
}, nil
}

func (d *inventoryDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
// Prevent panic if the provider has not been configured.
if req.ProviderData == nil {
return
}

client, ok := req.ProviderData.(*api.Client)

if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *dog.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)

return
}

d.p.dog = client
}

type inventoryDataSourceData struct {
ApiToken types.String `tfsdk:"api_token"`
Id types.String `tfsdk:"id"`
}

//type inventoryDataSource struct {
// provider provider
//}

func (d *inventoryDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var state InventoryList

res, statusCode, err := d.p.dog.GetInventories(nil)
if (statusCode < 200 || statusCode > 299) && statusCode != 404 {
resp.Diagnostics.AddError("Client Unsuccesful", fmt.Sprintf("Status Code: %d", statusCode))
}
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read inventories, got error: %s", err))
}
if resp.Diagnostics.HasError() {
return
}

// Set state
for _, api_inventory := range res {
inventory := ApiToInventory(api_inventory)
state = append(state, inventory)
}
diags := resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
6 changes: 2 additions & 4 deletions internal/provider/datasource_link.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/datasource"
api "github.com/relaypro-open/dog_api_golang/api"
)

Expand Down Expand Up @@ -44,7 +44,6 @@ type (
ServerNameIndication types.String `tfsdk:"server_name_indication"`
Verify types.String `tfsdk:"verify"`
}

)

var (
Expand All @@ -55,7 +54,6 @@ func NewLinkDataSource() datasource.DataSource {
return &linkDataSource{}
}


func (*linkDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_link"
}
Expand Down Expand Up @@ -102,7 +100,7 @@ func (d *linkDataSource) Configure(ctx context.Context, req datasource.Configure

type linkDataSourceData struct {
ApiToken types.String `tfsdk:"api_token"`
Id types.String `tfsdk:"id"`
Id types.String `tfsdk:"id"`
}

//type linkDataSource struct {
Expand Down
6 changes: 3 additions & 3 deletions internal/provider/datasource_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ type (
ProfileList []Profile

Profile struct {
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Version types.String `tfsdk:"version"`
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Version types.String `tfsdk:"version"`
}
)

Expand Down
11 changes: 5 additions & 6 deletions internal/provider/datasource_ruleset.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ type (
RulesetList []Ruleset

Ruleset struct {
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Rules *rulesetResourceRules `tfsdk:"rules"`
//ProfileId types.String `tfsdk:"profile_id" force:",omitempty"`
ProfileId types.String `tfsdk:"profile_id" json:"profile_id,omitempty"`

ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Rules *rulesetResourceRules `tfsdk:"rules"`
//ProfileId types.String `tfsdk:"profile_id" force:",omitempty"`
ProfileId types.String `tfsdk:"profile_id" json:"profile_id,omitempty"`
}

Rules struct {
Expand Down
7 changes: 2 additions & 5 deletions internal/provider/datasource_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/datasource"
api "github.com/relaypro-open/dog_api_golang/api"
)

Expand All @@ -31,8 +31,6 @@ type (
Ports []string `tfsdk:"ports"`
Protocol types.String `tfsdk:"protocol"`
}


)

var (
Expand All @@ -43,7 +41,6 @@ func NewServiceDataSource() datasource.DataSource {
return &serviceDataSource{}
}


func (*serviceDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_service"
}
Expand Down Expand Up @@ -90,7 +87,7 @@ func (d *serviceDataSource) Configure(ctx context.Context, req datasource.Config

type serviceDataSourceData struct {
ApiToken types.String `tfsdk:"api_token"`
Id types.String `tfsdk:"id"`
Id types.String `tfsdk:"id"`
}

//type serviceDataSource struct {
Expand Down
Loading

0 comments on commit 3b7ebad

Please sign in to comment.