Skip to content

Commit 7eff282

Browse files
authored
added functionality to support headers (#1)
1 parent 4d270fb commit 7eff282

File tree

9 files changed

+266
-8
lines changed

9 files changed

+266
-8
lines changed

README.md

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,50 @@
44
terraform-provider-curl2 is designed to help make HTTP(s) requests,
55
with additional support for providing JSON bodies and authentication headers.
66

7-
Key Features:
7+
* [Curl2 Provider Documentation](https://registry.terraform.io/providers/mehulgohil/curl2/latest/docs)
88

9+
## Key Features
910
HTTP Method Support:
1011
1. The custom provider allows you to perform various HTTP methods like GET, POST, PUT, DELETE, etc.
1112
2. JSON Body Support: You can provide JSON payloads as the request body for methods like POST or PUT.
1213
This enables you to send structured data to the API endpoints.
1314
3. Authentication Headers: The custom provider supports the inclusion of authentication headers in the HTTP requests.
1415
You can provide headers like API keys, tokens, or other authentication mechanisms required by the API.
16+
4. Custom Headers: The custom provider supports the inclusion of custom additional headers in the HTTP requests.
17+
18+
## Example
19+
20+
```hcl
21+
terraform {
22+
required_providers {
23+
curl2 = {
24+
source = "mehulgohil/curl2"
25+
version = "1.0.3"
26+
}
27+
}
28+
}
29+
30+
provider "curl2" {}
31+
32+
data "curl2" "getPosts" {
33+
http_method = "GET"
34+
uri = "https://jsonplaceholder.typicode.com/posts"
35+
# auth_type = "Basic"
36+
# basic_auth_username = "<UserName>"
37+
# basic_auth_password = "<Password>"
38+
# headers = {
39+
# Accept = "*/*"
40+
# }
41+
}
42+
43+
output "all_posts_response" {
44+
value = jsondecode(data.curl2.getPosts.response.body)
45+
}
46+
47+
output "all_posts_status" {
48+
value = data.curl2.getPosts.response.status_code
49+
}
50+
```
51+
52+
1553

curl2/data_source.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type curl2DataModelRequest struct {
3030
BearerToken types.String `tfsdk:"bearer_token"`
3131
BasicAuthUsername types.String `tfsdk:"basic_auth_username"`
3232
BasicAuthPassword types.String `tfsdk:"basic_auth_password"`
33+
Headers types.Map `tfsdk:"headers"`
3334
}
3435

3536
type curl2DataSource struct {
@@ -82,6 +83,10 @@ func (c *curl2DataSource) Schema(ctx context.Context, req datasource.SchemaReque
8283
Optional: true,
8384
Sensitive: true,
8485
},
86+
"headers": schema.MapAttribute{
87+
ElementType: types.StringType,
88+
Optional: true,
89+
},
8590
},
8691
}
8792
}
@@ -129,6 +134,10 @@ func (c *curl2DataSource) Read(ctx context.Context, req datasource.ReadRequest,
129134
return
130135
}
131136

137+
for eachHeaderKey, eachHeaderValue := range config.Headers.Elements() {
138+
newReq.Header.Set(eachHeaderKey, eachHeaderValue.String())
139+
}
140+
132141
if config.AuthType.ValueString() != "" {
133142
if config.AuthType.ValueString() == "Bearer" {
134143
if config.BearerToken.ValueString() == "" {

curl2/provider.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func (c *curl2Provider) Metadata(_ context.Context, _ provider.MetadataRequest,
2626
// Schema defines the provider-level schema for configuration data.
2727
func (c *curl2Provider) Schema(_ context.Context, _ provider.SchemaRequest, resp *provider.SchemaResponse) {
2828
resp.Schema = schema.Schema{
29-
Description: "Triggers HTTP(s) requests along with JSON body and authentication",
29+
Description: "Triggers HTTP(s) requests along with JSON body, authentication as well as custom headers",
3030
Attributes: map[string]schema.Attribute{},
3131
}
3232
}

docs/data-sources/curl2.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ terraform {
1717
required_providers {
1818
curl2 = {
1919
source = "mehulgohil/curl2"
20-
version = "1.0.2"
20+
version = "1.0.3"
2121
}
2222
}
2323
}
@@ -30,6 +30,9 @@ data "curl2" "getPosts" {
3030
# auth_type = "Basic"
3131
# basic_auth_username = "<UserName>"
3232
# basic_auth_password = "<Password>"
33+
# headers = {
34+
# Accept = "*/*"
35+
# }
3336
}
3437
3538
output "all_posts_response" {
@@ -46,6 +49,10 @@ data "curl2" "postPosts" {
4649
json = "{\"title\":\"foo\",\"body\":\"bar\",\"userId\":\"1\"}" //need the json in string format
4750
# auth_type = "Bearer"
4851
# bearer_token = "<Any Bearer Token>"
52+
# headers = {
53+
# Accept = "*/*"
54+
# Content-Type = "application/json"
55+
# }
4956
}
5057
5158
output "post_posts_output" {
@@ -67,6 +74,7 @@ output "post_posts_output" {
6774
- `basic_auth_password` (String, Sensitive) Password to be used for Authentication.
6875
- `basic_auth_username` (String) Username to be used for Basic Authentication.
6976
- `bearer_token` (String, Sensitive) Bearer Token to be used for Authentication.
77+
- `headers` (Map of String)
7078
- `json` (String) JSON object in string format if using POST, PUT or PATCH method.
7179

7280
### Read-Only

docs/index.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
page_title: "curl2 Provider"
44
subcategory: ""
55
description: |-
6-
Triggers HTTP(s) requests along with JSON body and authentication
6+
Triggers HTTP(s) requests along with JSON body, authentication as well as custom headers
77
---
88

99
# curl2 Provider
1010

11-
Triggers HTTP(s) requests along with JSON body and authentication
11+
Triggers HTTP(s) requests along with JSON body, authentication as well as custom headers
1212

1313
## Example Usage
1414

@@ -17,7 +17,7 @@ terraform {
1717
required_providers {
1818
curl2 = {
1919
source = "mehulgohil/curl2"
20-
version = "1.0.2"
20+
version = "1.0.3"
2121
}
2222
}
2323
}
@@ -27,6 +27,12 @@ provider "curl2" {}
2727
data "curl2" "getPosts" {
2828
http_method = "GET"
2929
uri = "https://jsonplaceholder.typicode.com/posts"
30+
# auth_type = "Basic"
31+
# basic_auth_username = "<UserName>"
32+
# basic_auth_password = "<Password>"
33+
# headers = {
34+
# Accept = "*/*"
35+
# }
3036
}
3137
3238
output "all_posts_response" {

examples/data-sources/curl2/data-source.tf

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ terraform {
22
required_providers {
33
curl2 = {
44
source = "mehulgohil/curl2"
5-
version = "1.0.2"
5+
version = "1.0.3"
66
}
77
}
88
}
@@ -15,6 +15,9 @@ data "curl2" "getPosts" {
1515
# auth_type = "Basic"
1616
# basic_auth_username = "<UserName>"
1717
# basic_auth_password = "<Password>"
18+
# headers = {
19+
# Accept = "*/*"
20+
# }
1821
}
1922

2023
output "all_posts_response" {
@@ -31,6 +34,10 @@ data "curl2" "postPosts" {
3134
json = "{\"title\":\"foo\",\"body\":\"bar\",\"userId\":\"1\"}" //need the json in string format
3235
# auth_type = "Bearer"
3336
# bearer_token = "<Any Bearer Token>"
37+
# headers = {
38+
# Accept = "*/*"
39+
# Content-Type = "application/json"
40+
# }
3441
}
3542

3643
output "post_posts_output" {

examples/provider/provider.tf

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ terraform {
22
required_providers {
33
curl2 = {
44
source = "mehulgohil/curl2"
5-
version = "1.0.2"
5+
version = "1.0.3"
66
}
77
}
88
}
@@ -12,6 +12,12 @@ provider "curl2" {}
1212
data "curl2" "getPosts" {
1313
http_method = "GET"
1414
uri = "https://jsonplaceholder.typicode.com/posts"
15+
# auth_type = "Basic"
16+
# basic_auth_username = "<UserName>"
17+
# basic_auth_password = "<Password>"
18+
# headers = {
19+
# Accept = "*/*"
20+
# }
1521
}
1622

1723
output "all_posts_response" {

go.mod

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,50 @@ require (
88
)
99

1010
require (
11+
github.com/Masterminds/goutils v1.1.1 // indirect
12+
github.com/Masterminds/semver/v3 v3.1.1 // indirect
13+
github.com/Masterminds/sprig/v3 v3.2.2 // indirect
14+
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
15+
github.com/armon/go-radix v1.0.0 // indirect
16+
github.com/bgentry/speakeasy v0.1.0 // indirect
1117
github.com/fatih/color v1.13.0 // indirect
1218
github.com/golang/protobuf v1.5.2 // indirect
19+
github.com/google/uuid v1.3.0 // indirect
20+
github.com/hashicorp/errwrap v1.1.0 // indirect
21+
github.com/hashicorp/go-checkpoint v0.5.0 // indirect
22+
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
1323
github.com/hashicorp/go-hclog v1.4.0 // indirect
24+
github.com/hashicorp/go-multierror v1.1.1 // indirect
1425
github.com/hashicorp/go-plugin v1.4.8 // indirect
1526
github.com/hashicorp/go-uuid v1.0.3 // indirect
27+
github.com/hashicorp/go-version v1.6.0 // indirect
28+
github.com/hashicorp/hc-install v0.5.0 // indirect
29+
github.com/hashicorp/terraform-exec v0.18.1 // indirect
30+
github.com/hashicorp/terraform-json v0.15.0 // indirect
31+
github.com/hashicorp/terraform-plugin-docs v0.14.1 // indirect
1632
github.com/hashicorp/terraform-plugin-go v0.14.3 // indirect
1733
github.com/hashicorp/terraform-registry-address v0.1.0 // indirect
1834
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734 // indirect
1935
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb // indirect
36+
github.com/huandu/xstrings v1.3.2 // indirect
37+
github.com/imdario/mergo v0.3.13 // indirect
2038
github.com/kr/pretty v0.3.0 // indirect
2139
github.com/mattn/go-colorable v0.1.13 // indirect
2240
github.com/mattn/go-isatty v0.0.16 // indirect
41+
github.com/mitchellh/cli v1.1.5 // indirect
42+
github.com/mitchellh/copystructure v1.2.0 // indirect
2343
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
44+
github.com/mitchellh/reflectwalk v1.0.2 // indirect
2445
github.com/oklog/run v1.0.0 // indirect
46+
github.com/posener/complete v1.2.3 // indirect
47+
github.com/russross/blackfriday v1.6.0 // indirect
48+
github.com/shopspring/decimal v1.3.1 // indirect
49+
github.com/spf13/cast v1.5.0 // indirect
2550
github.com/vmihailenco/msgpack/v4 v4.3.12 // indirect
2651
github.com/vmihailenco/tagparser v0.1.1 // indirect
52+
github.com/zclconf/go-cty v1.13.0 // indirect
53+
golang.org/x/crypto v0.5.0 // indirect
54+
golang.org/x/mod v0.7.0 // indirect
2755
golang.org/x/net v0.5.0 // indirect
2856
golang.org/x/sys v0.4.0 // indirect
2957
golang.org/x/text v0.7.0 // indirect

0 commit comments

Comments
 (0)