Skip to content

Commit

Permalink
REST API, decommission restapi_get. New data source "rest". Included …
Browse files Browse the repository at this point in the history
…OAUTH2 authentication
  • Loading branch information
Ariel Abuel committed Jul 22, 2023
1 parent d65bd23 commit 51c7b92
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 93 deletions.
98 changes: 83 additions & 15 deletions config/data_source_restapi_get.go → config/data_source_rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,26 @@ import (
)

type RequestParameters struct {
uri string
params []map[string]interface{}
headers []map[string]interface{}
user string
password string
method string
payload string
uri string
params []map[string]interface{}
headers []map[string]interface{}
user string
password string
authorization string
method string
payload string
}

type TokenRequestParameters struct {
uri string
params []map[string]interface{}
headers []map[string]interface{}
user string
password string
client_id string
client_secret string
grant_type string
payload string
}

func dataSourceRestApiGet() *schema.Resource {
Expand All @@ -34,6 +47,10 @@ func dataSourceRestApiGet() *schema.Resource {
Type: schema.TypeString,
Required: true,
},
"token_uri": {
Type: schema.TypeString,
Required: true,
},
"user": {
Type: schema.TypeString,
Optional: true,
Expand All @@ -42,11 +59,30 @@ func dataSourceRestApiGet() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},
"client_id": {
Type: schema.TypeString,
Optional: true,
},
"client_secret": {
Type: schema.TypeString,
Optional: true,
},
"grant_type": {
Type: schema.TypeString,
Optional: true,
Default: "password",
},
"authorization": {
Type: schema.TypeString,
Optional: true,
Default: "basic",
},
"param": dataSourceKeyValueSchema(),
"header": dataSourceKeyValueSchema(),
"method": {
Type: schema.TypeString,
Optional: true,
Default: "get",
},
"payload": {
Type: schema.TypeString,
Expand All @@ -65,16 +101,28 @@ func dataSourceRestApiRead(ctx context.Context, d *schema.ResourceData, m interf
var diags diag.Diagnostics

reqparm := new(RequestParameters)
reqparm.uri = d.Get("uri").(string)
reqparm.uri = strings.TrimSpace(d.Get("uri").(string))
reqparm.user = d.Get("user").(string)
reqparm.password = d.Get("password").(string)
reqparm.authorization = d.Get("authorization").(string)
reqparm.method = d.Get("method").(string)
reqparm.payload = d.Get("payload").(string)

tokenparm := new(TokenRequestParameters)
tokenparm.uri = strings.TrimSpace(d.Get("token_uri").(string))
tokenparm.user = d.Get("user").(string)
tokenparm.password = d.Get("password").(string)
tokenparm.client_id = d.Get("client_id").(string)
tokenparm.client_secret = d.Get("client_secret").(string)
tokenparm.grant_type = d.Get("grant_type").(string)

whiteSpace := regexp.MustCompile(`\s+`)
if whiteSpace.Match([]byte(reqparm.uri)) {
return diag.FromErr(fmt.Errorf("uri cannot contain whitespace. Got \"%s\"", reqparm.uri))
}
if whiteSpace.Match([]byte(tokenparm.uri)) {
return diag.FromErr(fmt.Errorf("token_uri cannot contain whitespace. Got \"%s\"", reqparm.uri))
}

if v, ok := d.GetOk("param"); ok {
reqparm.params = buildConfigDataSourceParams(v.(*schema.Set))
Expand All @@ -83,7 +131,30 @@ func dataSourceRestApiRead(ctx context.Context, d *schema.ResourceData, m interf
reqparm.headers = buildConfigDataSourceParams(v.(*schema.Set))
}

data, err := getRequest(reqparm)
if reqparm.authorization == "oauth2" {
if tokenparm.user == "" {
return diag.FromErr(fmt.Errorf("missing user for oath2 authentication"))
}
if tokenparm.password == "" {
return diag.FromErr(fmt.Errorf("missing password for oath2 authentication"))
}
if tokenparm.client_id == "" {
return diag.FromErr(fmt.Errorf("missing client_id for oath2 authentication"))
}
if tokenparm.client_secret == "" {
return diag.FromErr(fmt.Errorf("missing client_secret for oath2 authentication"))
}
if tokenparm.grant_type == "" {
return diag.FromErr(fmt.Errorf("missing grant_type for oath2 authentication"))
}
if tokenparm.uri == "" {
return diag.FromErr(fmt.Errorf("missing token_uri for oath2 authentication"))
}

}

data, err := createR
equest(reqparm)
if err != nil {
return diag.FromErr(err)
}
Expand All @@ -96,7 +167,7 @@ func dataSourceRestApiRead(ctx context.Context, d *schema.ResourceData, m interf
return diags
}

func getRequest(args *RequestParameters) (string, error) {
func createRequest(args *RequestParameters) (string, error) {
param := url.Values{}
for _, p := range args.params {
param.Add(p["Key"].(string), p["Value"].(string))
Expand All @@ -107,10 +178,7 @@ func getRequest(args *RequestParameters) (string, error) {
url = args.uri + "?" + param.Encode()
}

method := "GET"
if args.method != "" {
method = args.method
}
method := args.method
payload := strings.NewReader(``)
if args.payload != "" {
payload = strings.NewReader(args.payload)
Expand All @@ -122,7 +190,7 @@ func getRequest(args *RequestParameters) (string, error) {
}

// add basic authentication
if args.user != "" && args.password != "" {
if args.authorization == "basic" && args.user != "" && args.password != "" {
plainCred := args.user + ":" + args.password
base64Cred := base64.StdEncoding.EncodeToString([]byte(plainCred))
req.Header.Add("Authorization", "Basic "+base64Cred)
Expand Down
3 changes: 2 additions & 1 deletion config/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ func Provider() *schema.Provider {
DataSourcesMap: map[string]*schema.Resource{
"config_workbook": dataSourceConfigurationWorkbook(),
"config_ini": dataSourceIni(),
"config_restapi_get": dataSourceRestApiGet(),
"config_restapi_get": dataSourceRest(),
"config_rest": dataSourceRest(),
},
}
}
8 changes: 4 additions & 4 deletions docs/data-sources/restapi_get.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ data "config_restapi_get "general" {
### Example - Using with basic authentication credentials

```terraform
data "config_restapi_get "general" {
data "config_rest" "general" {
uri = "http://localhost:3000/posts"
user = "user"
password = "pass"
Expand All @@ -46,7 +46,7 @@ data "config_restapi_get "general" {
locals {
userpass = "${var.username}:${var.password}"
}
data "config_restapi_get "general" {
data "config_rest" "general" {
uri = "http://localhost:3000/posts"
header {
key = "Authorization"
Expand All @@ -57,7 +57,7 @@ data "config_restapi_get "general" {

### Example - Using with additional headers
```terraform
data "config_restapi_get "general" {
data "config_rest" "general" {
uri = "http://localhost:3000/posts"
header {
key = "Content-type"
Expand All @@ -72,7 +72,7 @@ data "config_restapi_get "general" {

### Example - Using POST method
```terraform
data "config_restapi_get" "post" {
data "config_rest" "post" {
uri = "http://localhost:3000/posts"
header {
key = "Content-Type"
Expand Down
66 changes: 33 additions & 33 deletions docs/data-sources/workbook.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,6 @@ description: |-

# config_workbook (Data Source)

### Example - Using a CSV

```terraform
data "config_workbook "csv" {
csv = <<-EOT
configuration_item,name,b_create,cidr_block
vpc,my_vpc,1,"10.0.0.0/16"
EOT
}
# reading from a csv file
data "config_workbook" "csv_file" {
csv = file("filename.csv")
filter {
name = "columnHeaderName"
values = ["value1","value2","value3"]
}
}
```

### Example - Using an Excel file

```terraform
Expand Down Expand Up @@ -59,31 +39,51 @@ data "config_workbook" "excel_vertical" {
}
```

### Example - Using a CSV with a config schema

### Example - Using an Excel with a config schema
```terraform
data "config_workbook" "csv_using_yaml" {
csv = file("filename.csv")
data "config_workbook" "excel_using_yaml" {
excel = "filename.xlsx"
worksheet = "Sheet1"
schema = file("schema.yaml")
}
data "config_workbook" "csv_using_json" {
csv = file("filename.csv")
data "config_workbook" "excel_using_json" {
excel = "filename.xlsx"
worksheet = "Sheet1"
schema = file("schema.json")
}
```

### Example - Using an Excel with a config schema
### Example - Using a CSV

```terraform
data "config_workbook" "excel_using_yaml" {
excel = "filename.xlsx"
worksheet = "Sheet1"
data "config_workbook "csv" {
csv = <<-EOT
configuration_item,name,b_create,cidr_block
vpc,my_vpc,1,"10.0.0.0/16"
EOT
}
# reading from a csv file
data "config_workbook" "csv_file" {
csv = file("filename.csv")
filter {
name = "columnHeaderName"
values = ["value1","value2","value3"]
}
}
```

### Example - Using a CSV with a config schema

```terraform
data "config_workbook" "csv_using_yaml" {
csv = file("filename.csv")
schema = file("schema.yaml")
}
data "config_workbook" "excel_using_json" {
excel = "filename.xlsx"
worksheet = "Sheet1"
data "config_workbook" "csv_using_json" {
csv = file("filename.csv")
schema = file("schema.json")
}
```
Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ data "config_workbook" "excel" {
worksheet = "Sheet1"
}
data "config_restapi_get "apidata" {
data "config_rest" "response_data" {
uri = "http://localhost:3000/posts"
}
Expand Down
49 changes: 14 additions & 35 deletions examples/configuration_workbook/main.tf
Original file line number Diff line number Diff line change
@@ -1,29 +1,14 @@
terraform {
required_providers {
config = {
version = "0.2.2"
source = "aa/test/config"
version = "0.2.8"
source = "alabuel/config"
}
}
}

provider "config" {}


data "config_workbook" "csv" {
csv = file("files/test.csv")
schema = file("files/config.yaml")
filter {
name = "name"
values = ["item_name1"]
}
}

data "config_workbook" "vm" {
csv = file("files/deployVM.csv")
configuration_item = "virtual_machine"
}

data "config_workbook" "excel" {
excel = "files/data.xlsx"
worksheet = "Config"
Expand Down Expand Up @@ -73,30 +58,24 @@ data "config_ini" "ini" {
ini = file("files/event.ini")
}

# output "horiz" {
# value = jsondecode(data.config_workbook.excel.json)
# }
data "config_workbook" "csv" {
csv = file("files/test.csv")
schema = file("files/config.yaml")
filter {
name = "name"
values = ["item_name1"]
}
}

# output "vert" {
# value = jsondecode(data.config_workbook.vexcel.json)
# }
data "config_workbook" "vm" {
csv = file("files/deployVM.csv")
configuration_item = "virtual_machine"
}

output "lookup" {
value = jsondecode(data.config_workbook.lookup.json)
}

# output "events" {
# value = jsondecode(data.config_workbook.vert.json)
# }

# output "csv" {
# value = jsondecode(data.config_workbook.csv.json)
# }

# output "vm" {
# value = jsondecode(data.config_workbook.vm.json)
# }

output "ini" {
value = jsondecode(data.config_ini.ini.json)
}
Loading

0 comments on commit 51c7b92

Please sign in to comment.