diff --git a/config/data_source_restapi_get.go b/config/data_source_rest.go similarity index 53% rename from config/data_source_restapi_get.go rename to config/data_source_rest.go index 4a7d59f..cb965f9 100644 --- a/config/data_source_restapi_get.go +++ b/config/data_source_rest.go @@ -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 { @@ -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, @@ -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, @@ -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)) @@ -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) } @@ -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)) @@ -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) @@ -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) diff --git a/config/provider.go b/config/provider.go index 8d19a93..aff8295 100644 --- a/config/provider.go +++ b/config/provider.go @@ -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(), }, } } diff --git a/docs/data-sources/restapi_get.md b/docs/data-sources/restapi_get.md index 2e1470d..b24b657 100644 --- a/docs/data-sources/restapi_get.md +++ b/docs/data-sources/restapi_get.md @@ -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" @@ -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" @@ -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" @@ -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" diff --git a/docs/data-sources/workbook.md b/docs/data-sources/workbook.md index cec4830..37c1286 100644 --- a/docs/data-sources/workbook.md +++ b/docs/data-sources/workbook.md @@ -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 @@ -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") } ``` diff --git a/docs/index.md b/docs/index.md index 853b642..3f70340 100644 --- a/docs/index.md +++ b/docs/index.md @@ -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" } diff --git a/examples/configuration_workbook/main.tf b/examples/configuration_workbook/main.tf index 83bf74f..223257a 100644 --- a/examples/configuration_workbook/main.tf +++ b/examples/configuration_workbook/main.tf @@ -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" @@ -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) } \ No newline at end of file diff --git a/examples/restapi/main.tf b/examples/restapi/main.tf index cdab277..87a37b2 100644 --- a/examples/restapi/main.tf +++ b/examples/restapi/main.tf @@ -1,15 +1,15 @@ terraform { required_providers { config = { - version = "0.2.2" - source = "aa/test/config" + version = "0.2.8" + source = "alabuel/config" } } } provider "config" {} -data "config_restapi_get" "test" { +data "config_rest" "test" { uri = "http://localhost:3000/posts" header { key = "Content-Type" @@ -25,7 +25,7 @@ data "config_restapi_get" "test" { } } -data "config_restapi_get" "post" { +data "config_rest" "post" { uri = "http://localhost:3000/posts" header { key = "Content-Type"