Skip to content

Commit 3b9cc7c

Browse files
authored
VAULT-37221: Update Terraform provider to support Agent (#1353)
* refactor radar resource, add detector_type to onboarding call * add comment * add changelog * updated Radar resource create timeout to 2 minutes * update hcp go sdk
1 parent fb32914 commit 3b9cc7c

File tree

9 files changed

+100
-76
lines changed

9 files changed

+100
-76
lines changed

.changelog/1353.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:improvement
2+
Updates hcp_vault_radar_source_github_cloud and hcp_vault_radar_source_github_enterprise to accept `detector_type`.
3+
```

docs/resources/vault_radar_source_github_cloud.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ resource "hcp_vault_radar_source_github_cloud" "example" {
2323
github_organization = "my-github-org"
2424
token = var.github_cloud_token
2525
project_id = "my-project-id"
26+
detector_type = "hcp"
2627
}
2728
```
2829

@@ -37,6 +38,7 @@ resource "hcp_vault_radar_source_github_cloud" "example" {
3738

3839
### Optional
3940

41+
- `detector_type` (String) The detector type which will monitor this resource. The default is HCP if not specified.
4042
- `project_id` (String) The ID of the HCP project where Vault Radar is located. If not specified, the project specified in the HCP Provider config block will be used, if configured.
4143

4244
### Read-Only

docs/resources/vault_radar_source_github_enterprise.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ resource "hcp_vault_radar_source_github_enterprise" "example" {
2424
github_organization = "my-github-org"
2525
token = var.github_enterprise_token
2626
project_id = "my-project-id"
27+
detector_type = "hcp"
2728
}
2829
```
2930

@@ -39,6 +40,7 @@ resource "hcp_vault_radar_source_github_enterprise" "example" {
3940

4041
### Optional
4142

43+
- `detector_type` (String) The detector type which will monitor this resource. The default is HCP if not specified.
4244
- `project_id` (String) The ID of the HCP project where Vault Radar is located. If not specified, the project specified in the HCP Provider config block will be used, if configured.
4345

4446
### Read-Only

examples/resources/hcp_vault_radar_source_github_cloud/resource.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ resource "hcp_vault_radar_source_github_cloud" "example" {
77
github_organization = "my-github-org"
88
token = var.github_cloud_token
99
project_id = "my-project-id"
10+
detector_type = "hcp"
1011
}

examples/resources/hcp_vault_radar_source_github_enterprise/resource.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ resource "hcp_vault_radar_source_github_enterprise" "example" {
88
github_organization = "my-github-org"
99
token = var.github_enterprise_token
1010
project_id = "my-project-id"
11+
detector_type = "hcp"
1112
}

internal/clients/vault_radar.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
)
1818

1919
func OnboardRadarSource(ctx context.Context, client *Client, projectID string, source dsrs.OnboardDataSourceBody) (*dsrs.OnboardDataSourceOK, error) {
20-
onboardParams := dsrs.NewOnboardDataSourceParams()
20+
onboardParams := dsrs.NewOnboardDataSourceParamsWithTimeout(2 * time.Minute) // gives datasources with "agent" detector type more time to complete
2121
onboardParams.Context = ctx
2222
onboardParams.LocationProjectID = projectID
2323
onboardParams.Body = source

internal/provider/vaultradar/radar_source.go

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@ import (
77
"context"
88
"fmt"
99

10+
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
1011
"github.com/hashicorp/terraform-plugin-framework/diag"
1112
"github.com/hashicorp/terraform-plugin-framework/resource"
1213
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
14+
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
15+
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
16+
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
1317
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
1418
"github.com/hashicorp/terraform-plugin-framework/types"
1519
"github.com/hashicorp/terraform-plugin-log/tflog"
@@ -24,14 +28,45 @@ var (
2428
_ resource.ResourceWithConfigure = &radarSourceResource{}
2529
)
2630

31+
var (
32+
baseSourceSchema = map[string]schema.Attribute{
33+
"project_id": schema.StringAttribute{
34+
Description: "The ID of the HCP project where Vault Radar is located. If not specified, the project specified in the HCP Provider config block will be used, if configured.",
35+
Optional: true,
36+
Computed: true,
37+
PlanModifiers: []planmodifier.String{
38+
stringplanmodifier.RequiresReplace(),
39+
stringplanmodifier.UseStateForUnknown(),
40+
},
41+
},
42+
"id": schema.StringAttribute{
43+
Computed: true,
44+
Description: "The ID of this resource.",
45+
PlanModifiers: []planmodifier.String{
46+
stringplanmodifier.UseStateForUnknown(),
47+
},
48+
},
49+
"detector_type": schema.StringAttribute{
50+
Optional: true,
51+
Description: "The detector type which will monitor this resource. The default is HCP if not specified.",
52+
PlanModifiers: []planmodifier.String{
53+
stringplanmodifier.RequiresReplace(),
54+
},
55+
Validators: []validator.String{
56+
stringvalidator.OneOf("hcp", "agent"),
57+
},
58+
},
59+
}
60+
)
61+
2762
// radarSourceResource is an implementation for configuring specific types Radar data sources.
2863
// Examples: hcp_vault_radar_source_github_cloud and hcp_vault_radar_source_github_enterprise make use of
2964
// this implementation to define resources with specific schemas, validation, and state details related to their types.
3065
type radarSourceResource struct {
3166
client *clients.Client
3267
TypeName string
3368
SourceType string
34-
ConnectionSchema schema.Schema
69+
ResourceSchema schema.Schema
3570
GetSourceFromPlan func(ctx context.Context, plan tfsdk.Plan) (radarSource, diag.Diagnostics)
3671
GetSourceFromState func(ctx context.Context, state tfsdk.State) (radarSource, diag.Diagnostics)
3772
}
@@ -41,7 +76,18 @@ func (r *radarSourceResource) Metadata(_ context.Context, req resource.MetadataR
4176
}
4277

4378
func (r *radarSourceResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
44-
resp.Schema = r.ConnectionSchema
79+
resp.Schema = schema.Schema{
80+
MarkdownDescription: r.ResourceSchema.MarkdownDescription,
81+
Attributes: r.ResourceSchema.Attributes,
82+
}
83+
84+
for k, v := range baseSourceSchema {
85+
// check to see if schema key already exists; if so, skip
86+
if _, exists := resp.Schema.Attributes[k]; exists {
87+
continue
88+
}
89+
resp.Schema.Attributes[k] = v
90+
}
4591
}
4692

4793
// radarSource is the minimal plan/state that a Radar source must have.
@@ -53,8 +99,26 @@ type radarSource interface {
5399
GetName() types.String
54100
GetConnectionURL() types.String
55101
GetToken() types.String
102+
GetDetectorType() types.String
56103
}
57104

105+
// base abstraction of Radar datasource model, partially implements radarSource interface
106+
type abstractSourceModel struct {
107+
ProjectID types.String `tfsdk:"project_id"`
108+
ID types.String `tfsdk:"id"`
109+
DetectorType types.String `tfsdk:"detector_type"`
110+
}
111+
112+
func (b *abstractSourceModel) GetProjectID() types.String { return b.ProjectID }
113+
114+
func (b *abstractSourceModel) SetProjectID(projectID types.String) { b.ProjectID = projectID }
115+
116+
func (b *abstractSourceModel) GetID() types.String { return b.ID }
117+
118+
func (b *abstractSourceModel) SetID(id types.String) { b.ID = id }
119+
120+
func (b *abstractSourceModel) GetDetectorType() types.String { return b.DetectorType }
121+
58122
func (r *radarSourceResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
59123
if req.ProviderData == nil {
60124
return
@@ -97,6 +161,10 @@ func (r *radarSourceResource) Create(ctx context.Context, req resource.CreateReq
97161
body.ConnectionURL = src.GetConnectionURL().ValueString()
98162
}
99163

164+
if !src.GetDetectorType().IsNull() {
165+
body.DetectorType = src.GetDetectorType().ValueString()
166+
}
167+
100168
res, err := clients.OnboardRadarSource(ctx, r.client, projectID, body)
101169
if err != nil {
102170
resp.Diagnostics.AddError("Error creating Radar source", err.Error())

internal/provider/vaultradar/resource_radar_source_github_cloud.go

Lines changed: 10 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ import (
2121

2222
func NewSourceGitHubCloudResource() resource.Resource {
2323
return &radarSourceResource{
24-
TypeName: "_vault_radar_source_github_cloud",
25-
SourceType: "github_cloud",
26-
ConnectionSchema: githubCloudSourceSchema,
24+
TypeName: "_vault_radar_source_github_cloud",
25+
SourceType: "github_cloud",
26+
ResourceSchema: githubCloudSourceSchema,
2727
GetSourceFromPlan: func(ctx context.Context, plan tfsdk.Plan) (radarSource, diag.Diagnostics) {
28-
var data githubCloudSourceData
28+
var data githubCloudSourceModel
2929
diags := plan.Get(ctx, &data)
3030
return &data, diags
3131
},
3232
GetSourceFromState: func(ctx context.Context, state tfsdk.State) (radarSource, diag.Diagnostics) {
33-
var data githubCloudSourceData
33+
var data githubCloudSourceModel
3434
diags := state.Get(ctx, &data)
3535
return &data, diags
3636
}}
@@ -39,13 +39,6 @@ func NewSourceGitHubCloudResource() resource.Resource {
3939
var githubCloudSourceSchema = schema.Schema{
4040
MarkdownDescription: "This terraform resource manages a GitHub Cloud data source lifecycle in Vault Radar.",
4141
Attributes: map[string]schema.Attribute{
42-
"id": schema.StringAttribute{
43-
Computed: true,
44-
Description: "The ID of this resource.",
45-
PlanModifiers: []planmodifier.String{
46-
stringplanmodifier.UseStateForUnknown(),
47-
},
48-
},
4942
"github_organization": schema.StringAttribute{
5043
Description: `GitHub organization Vault Radar will monitor. Example: type "octocat" for the org https://github.com/octocat`,
5144
Required: true,
@@ -64,37 +57,17 @@ var githubCloudSourceSchema = schema.Schema{
6457
Required: true,
6558
Sensitive: true,
6659
},
67-
68-
// Optional inputs
69-
"project_id": schema.StringAttribute{
70-
Description: "The ID of the HCP project where Vault Radar is located. If not specified, the project specified in the HCP Provider config block will be used, if configured.",
71-
Optional: true,
72-
Computed: true,
73-
PlanModifiers: []planmodifier.String{
74-
stringplanmodifier.RequiresReplace(),
75-
stringplanmodifier.UseStateForUnknown(),
76-
},
77-
},
7860
},
7961
}
8062

81-
type githubCloudSourceData struct {
82-
ID types.String `tfsdk:"id"`
63+
type githubCloudSourceModel struct {
64+
abstractSourceModel
8365
GitHubOrganization types.String `tfsdk:"github_organization"`
8466
Token types.String `tfsdk:"token"`
85-
ProjectID types.String `tfsdk:"project_id"`
8667
}
8768

88-
func (d *githubCloudSourceData) GetProjectID() types.String { return d.ProjectID }
89-
90-
func (d *githubCloudSourceData) SetProjectID(projectID types.String) { d.ProjectID = projectID }
91-
92-
func (d *githubCloudSourceData) GetID() types.String { return d.ID }
93-
94-
func (d *githubCloudSourceData) SetID(id types.String) { d.ID = id }
95-
96-
func (d *githubCloudSourceData) GetName() types.String { return d.GitHubOrganization }
69+
func (d *githubCloudSourceModel) GetName() types.String { return d.GitHubOrganization }
9770

98-
func (d *githubCloudSourceData) GetConnectionURL() types.String { return basetypes.NewStringNull() }
71+
func (d *githubCloudSourceModel) GetConnectionURL() types.String { return basetypes.NewStringNull() }
9972

100-
func (d *githubCloudSourceData) GetToken() types.String { return d.Token }
73+
func (d *githubCloudSourceModel) GetToken() types.String { return d.Token }

internal/provider/vaultradar/resource_radar_source_github_enterprise.go

Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@ import (
2020

2121
func NewSourceGitHubEnterpriseResource() resource.Resource {
2222
return &radarSourceResource{
23-
TypeName: "_vault_radar_source_github_enterprise",
24-
SourceType: "github_enterprise",
25-
ConnectionSchema: githubEnterpriseSourceSchema,
23+
TypeName: "_vault_radar_source_github_enterprise",
24+
SourceType: "github_enterprise",
25+
ResourceSchema: githubEnterpriseSourceSchema,
2626
GetSourceFromPlan: func(ctx context.Context, plan tfsdk.Plan) (radarSource, diag.Diagnostics) {
27-
var data githubEnterpriseSourceData
27+
var data githubEnterpriseSourceModel
2828
diags := plan.Get(ctx, &data)
2929
return &data, diags
3030
},
3131
GetSourceFromState: func(ctx context.Context, state tfsdk.State) (radarSource, diag.Diagnostics) {
32-
var data githubEnterpriseSourceData
32+
var data githubEnterpriseSourceModel
3333
diags := state.Get(ctx, &data)
3434
return &data, diags
3535
}}
@@ -39,13 +39,6 @@ func NewSourceGitHubEnterpriseResource() resource.Resource {
3939
var githubEnterpriseSourceSchema = schema.Schema{
4040
MarkdownDescription: "This terraform resource manages a GitHub Enterprise Server data source lifecycle in Vault Radar.",
4141
Attributes: map[string]schema.Attribute{
42-
"id": schema.StringAttribute{
43-
Computed: true,
44-
Description: "The ID of this resource.",
45-
PlanModifiers: []planmodifier.String{
46-
stringplanmodifier.UseStateForUnknown(),
47-
},
48-
},
4942
"domain_name": schema.StringAttribute{
5043
Description: "Fully qualified domain name of the server. (Example: myserver.acme.com)",
5144
Required: true,
@@ -77,37 +70,18 @@ var githubEnterpriseSourceSchema = schema.Schema{
7770
Required: true,
7871
Sensitive: true,
7972
},
80-
// Optional inputs
81-
"project_id": schema.StringAttribute{
82-
Description: "The ID of the HCP project where Vault Radar is located. If not specified, the project specified in the HCP Provider config block will be used, if configured.",
83-
Optional: true,
84-
Computed: true,
85-
PlanModifiers: []planmodifier.String{
86-
stringplanmodifier.RequiresReplace(),
87-
stringplanmodifier.UseStateForUnknown(),
88-
},
89-
},
9073
},
9174
}
9275

93-
type githubEnterpriseSourceData struct {
94-
ID types.String `tfsdk:"id"`
76+
type githubEnterpriseSourceModel struct {
77+
abstractSourceModel
9578
DomainName types.String `tfsdk:"domain_name"`
9679
GitHubOrganization types.String `tfsdk:"github_organization"`
9780
Token types.String `tfsdk:"token"`
98-
ProjectID types.String `tfsdk:"project_id"`
9981
}
10082

101-
func (d *githubEnterpriseSourceData) GetProjectID() types.String { return d.ProjectID }
102-
103-
func (d *githubEnterpriseSourceData) SetProjectID(projectID types.String) { d.ProjectID = projectID }
104-
105-
func (d *githubEnterpriseSourceData) GetID() types.String { return d.ID }
106-
107-
func (d *githubEnterpriseSourceData) SetID(id types.String) { d.ID = id }
108-
109-
func (d *githubEnterpriseSourceData) GetName() types.String { return d.GitHubOrganization }
83+
func (d *githubEnterpriseSourceModel) GetName() types.String { return d.GitHubOrganization }
11084

111-
func (d *githubEnterpriseSourceData) GetConnectionURL() types.String { return d.DomainName }
85+
func (d *githubEnterpriseSourceModel) GetConnectionURL() types.String { return d.DomainName }
11286

113-
func (d *githubEnterpriseSourceData) GetToken() types.String { return d.Token }
87+
func (d *githubEnterpriseSourceModel) GetToken() types.String { return d.Token }

0 commit comments

Comments
 (0)