Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
therve committed Jan 29, 2024
1 parent 77ab73e commit ca41a84
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 19 deletions.
5 changes: 5 additions & 0 deletions datadog/fwprovider/framework_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,11 @@ func defaultConfigureFunc(p *FrameworkProvider, request *provider.ConfigureReque
ddClientConfig.UserAgent = utils.GetUserAgentFramework(ddClientConfig.UserAgent, request.TerraformVersion)
ddClientConfig.Debug = logging.IsDebugOrHigher()

ddClientConfig.SetUnstableOperationEnabled("v2.CreateOpenAPI", true)
ddClientConfig.SetUnstableOperationEnabled("v2.UpdateOpenAPI", true)
ddClientConfig.SetUnstableOperationEnabled("v2.GetOpenAPI", true)
ddClientConfig.SetUnstableOperationEnabled("v2.DeleteOpenAPI", true)

if !config.ApiUrl.IsNull() && config.ApiUrl.ValueString() != "" {
parsedAPIURL, parseErr := url.Parse(config.ApiUrl.ValueString())
if parseErr != nil {
Expand Down
1 change: 1 addition & 0 deletions datadog/tests/cassettes/TestAccOpenapiApiBasic.freeze
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2024-01-29T13:46:34.377795845+01:00
114 changes: 114 additions & 0 deletions datadog/tests/cassettes/TestAccOpenapiApiBasic.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
---
version: 2
interactions:
- id: 0
request:
proto: HTTP/1.1
proto_major: 1
proto_minor: 1
content_length: 446
transfer_encoding: []
trailer: {}
host: api.datadoghq.com
remote_addr: ""
request_uri: ""
body: "--b83b2fd69da78bf28017af4b8c09378bf61fa1caa18e15bf1b49ab6e16f4\r\nContent-Disposition: form-data; name=\"openapi_spec_file\"; filename=\".\"\r\nContent-Type: application/octet-stream\r\n\r\ninfo:\n contact:\n name: API team\n description: My API description.\n title: tf-TestAccOpenapiApiBasic-local-1706532394\nopenapi: 3.0.0\npaths:\n /api/my-api:\n get:\n description: An endpoint\n\r\n--b83b2fd69da78bf28017af4b8c09378bf61fa1caa18e15bf1b49ab6e16f4--\r\n"
form: {}
headers:
Accept:
- application/json
Content-Type:
- multipart/form-data; boundary=b83b2fd69da78bf28017af4b8c09378bf61fa1caa18e15bf1b49ab6e16f4
url: https://api.datadoghq.com/api/v2/apicatalog/openapi
method: POST
response:
proto: HTTP/1.1
proto_major: 1
proto_minor: 1
transfer_encoding: []
trailer: {}
content_length: 104
uncompressed: false
body: '{"data":{"id":"3326a3a7-6d74-48b7-9a63-bd7b5656ed6f","type":"api","attributes":{"failed_endpoints":[]}}}'
headers:
Content-Type:
- application/vnd.api+json
status: 201 Created
code: 201
duration: 409.277916ms
- id: 1
request:
proto: HTTP/1.1
proto_major: 1
proto_minor: 1
content_length: 0
transfer_encoding: []
trailer: {}
host: api.datadoghq.com
remote_addr: ""
request_uri: ""
body: ""
form: {}
headers:
Accept:
- application/json
url: https://api.datadoghq.com/api/v2/apicatalog/api/3326a3a7-6d74-48b7-9a63-bd7b5656ed6f/openapi
method: GET
response:
proto: HTTP/1.1
proto_major: 1
proto_minor: 1
transfer_encoding: []
trailer: {}
content_length: 200
uncompressed: false
body: |
info:
contact:
name: API team
description: My API description.
title: tf-TestAccOpenapiApiBasic-local-1706532394
openapi: 3.0.0
paths:
/api/my-api:
get:
description: An endpoint
headers:
Content-Type:
- multipart/form-data
status: 200 OK
code: 200
duration: 113.217858ms
- id: 2
request:
proto: HTTP/1.1
proto_major: 1
proto_minor: 1
content_length: 0
transfer_encoding: []
trailer: {}
host: api.datadoghq.com
remote_addr: ""
request_uri: ""
body: ""
form: {}
headers:
Accept:
- '*/*'
url: https://api.datadoghq.com/api/v2/apicatalog/api/3326a3a7-6d74-48b7-9a63-bd7b5656ed6f
method: DELETE
response:
proto: HTTP/1.1
proto_major: 1
proto_minor: 1
transfer_encoding: []
trailer: {}
content_length: 0
uncompressed: false
body: ""
headers:
Content-Type:
- application/vnd.api+json
status: 204 No Content
code: 204
duration: 117.394279ms
5 changes: 5 additions & 0 deletions datadog/tests/framework_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ func buildFrameworkDatadogClient(ctx context.Context, httpClient *http.Client) *
//Datadog API config.HTTPClient
config := common.NewConfiguration()

config.SetUnstableOperationEnabled("v2.CreateOpenAPI", true)
config.SetUnstableOperationEnabled("v2.UpdateOpenAPI", true)
config.SetUnstableOperationEnabled("v2.GetOpenAPI", true)
config.SetUnstableOperationEnabled("v2.DeleteOpenAPI", true)

if ctx.Value("http_retry_enable") == true {
config.RetryConfiguration.EnableRetry = true
}
Expand Down
1 change: 1 addition & 0 deletions datadog/tests/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ var testFiles2EndpointTags = map[string]string{
"tests/resource_datadog_dashboard_topology_map_test": "dashboards",
"tests/resource_datadog_dashboard_trace_service_test": "dashboards",
"tests/resource_datadog_dashboard_treemap_test": "dashboards",
"tests/resource_datadog_openapi_api_test": "apimanagement",
"tests/resource_datadog_powerpack_test": "powerpacks",
"tests/resource_datadog_powerpack_alert_graph_test": "powerpacks",
"tests/resource_datadog_powerpack_alert_value_test": "powerpacks",
Expand Down
39 changes: 32 additions & 7 deletions datadog/tests/resource_datadog_openapi_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,24 @@ import (
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"

"github.com/terraform-providers/terraform-provider-datadog/datadog"
"github.com/google/uuid"
"github.com/terraform-providers/terraform-provider-datadog/datadog/fwprovider"
"github.com/terraform-providers/terraform-provider-datadog/datadog/internal/utils"
)

const apiContent = `
info:
contact:
name: API team
description: My API description.
title: %s
openapi: 3.0.0
paths:
/api/my-api:
get:
description: An endpoint
`

func TestAccOpenapiApiBasic(t *testing.T) {
t.Parallel()
ctx, providers, accProviders := testAccFrameworkMuxProviders(context.Background(), t)
Expand All @@ -33,10 +46,20 @@ func TestAccOpenapiApiBasic(t *testing.T) {
}

func testAccCheckDatadogOpenapiApi(uniq string) string {
// Update me to make use of the unique value
return fmt.Sprintf(`resource "datadog_openapi_api" "foo" {
spec_file = "UPDATE ME"
body = "UPDATE ME"
return fmt.Sprintf(`
resource "datadog_openapi_api" "test-api" {
spec = <<EOT
info:
contact:
name: API team
description: My API description.
title: %s
openapi: 3.0.0
paths:
/api/my-api:
get:
description: An endpoint
EOT
}`, uniq)
}

Expand All @@ -60,7 +83,8 @@ func OpenapiApiDestroyHelper(auth context.Context, s *terraform.State, apiInstan
}
id := r.Primary.ID

_, httpResp, err := apiInstances.GetAPIManagementApiV2().GetOpenAPI(auth, id)
uuid, _ := uuid.Parse(id)
_, httpResp, err := apiInstances.GetAPIManagementApiV2().GetOpenAPI(auth, uuid)
if err != nil {
if httpResp != nil && httpResp.StatusCode == 404 {
return nil
Expand Down Expand Up @@ -93,7 +117,8 @@ func openapiApiExistsHelper(auth context.Context, s *terraform.State, apiInstanc
}
id := r.Primary.ID

_, httpResp, err := apiInstances.GetAPIManagementApiV2().GetOpenAPI(auth, id)
uuid, _ := uuid.Parse(id)
_, httpResp, err := apiInstances.GetAPIManagementApiV2().GetOpenAPI(auth, uuid)
if err != nil {
return utils.TranslateClientError(err, httpResp, "error retrieving OpenapiApi")
}
Expand Down
7 changes: 3 additions & 4 deletions examples/resources/datadog_openapi_api/resource.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Create new openapi_api resource

resource "datadog_openapi_api" "foo" {
spec_file = "UPDATE ME"
body = "UPDATE ME"
}
resource "datadog_openapi_api" "my-api" {
spec = file("./path/my-api.yaml")
}
6 changes: 2 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
module github.com/terraform-providers/terraform-provider-datadog

require (
github.com/DataDog/datadog-api-client-go/v2 v2.21.1-0.20240125185137-c5d2119fede6
github.com/DataDog/datadog-api-client-go/v2 v2.21.1-0.20240126170449-0d471e4f5276
github.com/DataDog/dd-sdk-go-testing v0.0.0-20211116174033-1cd082e322ad
github.com/google/uuid v1.5.0
github.com/hashicorp/go-cleanhttp v0.5.2
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
github.com/hashicorp/terraform-plugin-docs v0.16.0
Expand Down Expand Up @@ -41,7 +42,6 @@ require (
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/pprof v0.0.0-20210423192551-a2663126120b // indirect
github.com/google/uuid v1.3.1 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-checkpoint v0.5.0 // indirect
github.com/hashicorp/go-hclog v1.5.0 // indirect
Expand Down Expand Up @@ -97,5 +97,3 @@ require (
)

go 1.20

replace github.com/DataDog/datadog-api-client-go/v2 => /home/therve/Projects/datadog-api-client-go
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/DataDog/datadog-api-client-go/v2 v2.21.1-0.20240125185137-c5d2119fede6 h1:KlX1O7Jfz0GE9LzEIrP6xXzimChrUAtvNQNBD7BSdYg=
github.com/DataDog/datadog-api-client-go/v2 v2.21.1-0.20240125185137-c5d2119fede6/go.mod h1:lHlfhsNQ2qZclvpVQTfrGowUDIdIzAao38A05f9EQpc=
github.com/DataDog/datadog-api-client-go/v2 v2.21.1-0.20240126170449-0d471e4f5276 h1:f9Q2bB8fbgbjgw2lO2pbf/L6N1/jza6gKtkNw9WOxL4=
github.com/DataDog/datadog-api-client-go/v2 v2.21.1-0.20240126170449-0d471e4f5276/go.mod h1:QKOu6vscsh87fMY1lHfLEmNSunyXImj8BUaUWJXOehc=
github.com/DataDog/datadog-go v4.4.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ=
github.com/DataDog/datadog-go v4.8.3+incompatible h1:fNGaYSuObuQb5nzeTQqowRAd9bpDIRRV4/gUtIBjh8Q=
github.com/DataDog/datadog-go v4.8.3+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ=
Expand Down Expand Up @@ -97,8 +97,8 @@ github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
Expand Down

0 comments on commit ca41a84

Please sign in to comment.