Skip to content

Commit

Permalink
ruleset acceptance tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
dgulinobw committed Feb 28, 2023
1 parent 7fa8c0b commit c8a8f2f
Show file tree
Hide file tree
Showing 16 changed files with 940 additions and 203 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
TEST?=$$(go list ./... | grep -v 'vendor')
TEST?=$$(go list ./... | grep -v 'vendor' | grep -v 'disable')
HOSTNAME=github.com
NAMESPACE=relaypro-open
NAME=dog
Expand Down
14 changes: 6 additions & 8 deletions internal/provider/datasource_profile.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,12 +19,11 @@ type (
ProfileList []Profile

Profile struct {
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
RuleId types.String `tfsdk:"rule_id"`
Version types.String `tfsdk:"version"`
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
RulesetId types.String `tfsdk:"ruleset_id"`
Version types.String `tfsdk:"version"`
}

)

var (
Expand All @@ -35,7 +34,6 @@ func NewProfileDataSource() datasource.DataSource {
return &profileDataSource{}
}


func (*profileDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_profile"
}
Expand Down Expand Up @@ -82,7 +80,7 @@ func (d *profileDataSource) Configure(ctx context.Context, req datasource.Config

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

//type profileDataSource struct {
Expand Down
136 changes: 136 additions & 0 deletions internal/provider/datasource_profile.go.old
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package dog

import (
"context"
"fmt"

"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"
)

type (
profileDataSource struct {
p dogProvider
}

ProfileList []Profile

Profile struct {
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Rules *Rules `tfsdk:"rules"`
Version types.String `tfsdk:"version"`
}
Rules struct {
Inbound []*Rule `tfsdk:"inbound"`
Outbound []*Rule `tfsdk:"outbound"`
}

Rule struct {
Action types.String `tfsdk:"action"`
Active types.Bool `tfsdk:"active"`
Comment types.String `tfsdk:"comment"`
Environments []string `tfsdk:"environments"`
Group types.String `tfsdk:"group"`
GroupType types.String `tfsdk:"group_type"`
Interface types.String `tfsdk:"interface"`
Log types.Bool `tfsdk:"log"`
LogPrefix types.String `tfsdk:"log_prefix"`
Order types.Int64 `tfsdk:"order"`
Service types.String `tfsdk:"service"`
States []string `tfsdk:"states"`
Type types.String `tfsdk:"type"`
}

)

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

func NewProfileDataSource() datasource.DataSource {
return &profileDataSource{}
}


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

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

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

func (d *profileDataSource) 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 profileDataSourceData struct {
ApiKey types.String `tfsdk:"api_key"`
Id types.String `tfsdk:"id"`
}

//type profileDataSource struct {
// provider provider
//}

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

res, statusCode, err := d.p.dog.GetProfiles(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 profiles, got error: %s", err))
}
if resp.Diagnostics.HasError() {
return
}

// Set state
for _, api_profile := range res {
profile := ApiToProfile(api_profile)
state = append(state, profile)
}
diags := resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
1 change: 0 additions & 1 deletion internal/provider/datasource_ruleset.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ type (
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Rules *rulesetResourceRules `tfsdk:"rules"`
Version types.String `tfsdk:"version"`
}

Rules struct {
Expand Down
38 changes: 19 additions & 19 deletions internal/provider/resource_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,24 +60,24 @@ func (*groupResource) GetSchema(ctx context.Context) (tfsdk.Schema, diag.Diagnos
Optional: true,
Type: types.StringType,
},
"ec2_security_group_ids": {
MarkdownDescription: "List of EC2 Security Groups to control",
Optional: true,
Attributes: tfsdk.ListNestedAttributes(map[string]tfsdk.Attribute{
"region": {
MarkdownDescription: "EC2 Region",
Required: true,
Type: types.StringType,
},
"sgid": {
MarkdownDescription: "EC2 Security Group ID",
Required: true,
Type: types.StringType,
},
}),
},
"ec2_security_group_ids": {
MarkdownDescription: "List of EC2 Security Groups to control",
Optional: true,
Attributes: tfsdk.ListNestedAttributes(map[string]tfsdk.Attribute{
"region": {
MarkdownDescription: "EC2 Region",
Required: true,
Type: types.StringType,
},
"sgid": {
MarkdownDescription: "EC2 Security Group ID",
Required: true,
Type: types.StringType,
},
}),
},
"vars": {
MarkdownDescription: "Arbitrary collection of variables used for inventory",
MarkdownDescription: "Arbitrary collection of variables used for inventory",
Type: types.MapType{ElemType: types.StringType},
Optional: true,
},
Expand Down Expand Up @@ -148,7 +148,7 @@ func GroupToCreateRequest(plan groupResourceData) api.GroupCreateRequest {
ProfileName: plan.ProfileName,
ProfileVersion: plan.ProfileVersion,
Ec2SecurityGroupIds: newEc2SecurityGroupIds,
Vars: plan.Vars,
Vars: plan.Vars,
}
return newGroup
}
Expand All @@ -169,7 +169,7 @@ func GroupToUpdateRequest(plan groupResourceData) api.GroupUpdateRequest {
ProfileName: plan.ProfileName,
ProfileVersion: plan.ProfileVersion,
Ec2SecurityGroupIds: newEc2SecurityGroupIds,
Vars: plan.Vars,
Vars: plan.Vars,
}
return newGroup
}
Expand Down
4 changes: 2 additions & 2 deletions internal/provider/resource_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (*profileResource) GetSchema(ctx context.Context) (tfsdk.Schema, diag.Diagn
Required: true,
Type: types.StringType,
},
"rule_id": {
"ruleset_id": {
MarkdownDescription: "Rule Id",
Required: true,
Type: types.StringType,
Expand Down Expand Up @@ -121,7 +121,7 @@ func ApiToProfile(profile api.Profile) Profile {
//Created: types.Int64{Value: int64(profile.Created)},
ID: types.String{Value: profile.ID},
Name: types.String{Value: profile.Name},
RuleId: types.String{Value: profile.RulesetId},
RulesetId: types.String{Value: profile.RulesetId},
Version: types.String{Value: profile.Version},
}
return h
Expand Down
Loading

0 comments on commit c8a8f2f

Please sign in to comment.