Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[datadog_downtime_schedule] Add support for Downtime V2 #2023

Merged
merged 22 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
260b5cb
generate downtimes
skarimo Jul 6, 2023
e9569e0
Implement datadog_downtime_schedule from the generated code
carlmartensen Jul 24, 2023
34d9026
Don't deprecate datadog_downtime yet
carlmartensen Jul 25, 2023
12d2d03
Set one-time schedule start to now when converting from recurring sch…
carlmartensen Jul 25, 2023
9e23652
Specify the resource is in private beta
carlmartensen Jul 25, 2023
9405cad
Change timeformat plan modifier to validator
carlmartensen Jul 25, 2023
50cb2fc
Update docs vis make docs
carlmartensen Jul 25, 2023
3f225c4
Update recordings
carlmartensen Jul 26, 2023
1b274f1
Rename validator
carlmartensen Jul 26, 2023
a1fd7b0
Bump client
carlmartensen Jul 26, 2023
64bf150
Fix unrelated compliation error (TODO revert)
carlmartensen Jul 26, 2023
5a0b570
Add test coverage for changing schedules
carlmartensen Jul 26, 2023
9de8c77
Use UTC for test now comparison
carlmartensen Jul 26, 2023
334752d
Update recordings
carlmartensen Jul 26, 2023
5dff5c1
Add more info for now comparison test failure
carlmartensen Jul 26, 2023
5510857
Ensure timezone diff doesn't affect test
carlmartensen Jul 26, 2023
e8ce239
Handle tf bug where removing a block causes a plan error
carlmartensen Jul 26, 2023
850b7b3
use frozen time in replayed tests
nkzou Jul 26, 2023
b99c813
Merge remote-tracking branch 'origin/master' into carl/downtime-schedule
carlmartensen Jul 27, 2023
2edf630
Run go mod tidy
carlmartensen Jul 27, 2023
ad13d1c
fix panics and add import test
skarimo Aug 1, 2023
cfe4122
fix state setting for list on import
skarimo Aug 1, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion datadog/fwprovider/data_source_datadog_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (d *datadogTeamDataSource) Read(ctx context.Context, req datasource.ReadReq
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
}

func (r *datadogTeamDataSource) updateState(state *datadogTeamDataSourceModel, teamData *datadogV2.TeamData) {
func (r *datadogTeamDataSource) updateState(state *datadogTeamDataSourceModel, teamData *datadogV2.Team) {
nkzou marked this conversation as resolved.
Show resolved Hide resolved
state.ID = types.StringValue(teamData.GetId())
attributes := teamData.GetAttributes()

Expand Down
7 changes: 7 additions & 0 deletions datadog/fwprovider/framework_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var (

var Resources = []func() resource.Resource{
NewAPIKeyResource,
NewDowntimeScheduleResource,
NewIntegrationCloudflareAccountResource,
NewIntegrationConfluentAccountResource,
NewIntegrationConfluentResourceResource,
Expand Down Expand Up @@ -345,6 +346,12 @@ func defaultConfigureFunc(p *FrameworkProvider, request *provider.ConfigureReque
ddClientConfig.UserAgent = utils.GetUserAgentFramework(ddClientConfig.UserAgent, request.TerraformVersion)
ddClientConfig.Debug = logging.IsDebugOrHigher()

// Temp - enable Downtime v2 which is currently in private beta
ddClientConfig.SetUnstableOperationEnabled("v2.CancelDowntime", true)
ddClientConfig.SetUnstableOperationEnabled("v2.CreateDowntime", true)
ddClientConfig.SetUnstableOperationEnabled("v2.GetDowntime", true)
ddClientConfig.SetUnstableOperationEnabled("v2.UpdateDowntime", true)

if !config.ApiUrl.IsNull() && config.ApiUrl.ValueString() != "" {
parsedAPIURL, parseErr := url.Parse(config.ApiUrl.ValueString())
if parseErr != nil {
Expand Down
593 changes: 593 additions & 0 deletions datadog/fwprovider/resource_datadog_downtime_schedule.go

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package planmodifiers

import (
"context"

"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
)

type removeBlockModifier struct {
}

func RemoveBlockModifier() planmodifier.Object {
return removeBlockModifier{}
}

func (m removeBlockModifier) Description(context.Context) string {
return "Set removed block to null."
}

func (m removeBlockModifier) MarkdownDescription(ctx context.Context) string {
return m.Description(ctx)
}

func (m removeBlockModifier) PlanModifyObject(ctx context.Context, req planmodifier.ObjectRequest, resp *planmodifier.ObjectResponse) {
// Older versions of terraform have a bug where removing a block results in 'planned for existence but config wants absence'.
// To work around this we can set the block to null.
// Reference: https://github.com/hashicorp/terraform/issues/32460
// Reference: https://github.com/hashicorp/terraform-plugin-framework/issues/603#issuecomment-1371358108
if req.ConfigValue.IsNull() {
resp.PlanValue = req.ConfigValue
}
}
9 changes: 9 additions & 0 deletions datadog/internal/utils/api_instances_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type ApiInstances struct {
cloudWorkloadSecurityApiV2 *datadogV2.CloudWorkloadSecurityApi
confluentCloudApiV2 *datadogV2.ConfluentCloudApi
dashboardListsApiV2 *datadogV2.DashboardListsApi
downtimesApiV2 *datadogV2.DowntimesApi
eventsApiV2 *datadogV2.EventsApi
fastlyIntegrationApiV2 *datadogV2.FastlyIntegrationApi
gcpStsIntegrationApiV2 *datadogV2.GCPIntegrationApi
Expand Down Expand Up @@ -351,6 +352,14 @@ func (i *ApiInstances) GetCloudWorkloadSecurityApiV2() *datadogV2.CloudWorkloadS
return i.cloudWorkloadSecurityApiV2
}

// GetDowntimesApiV2 get instance of DowntimesApi
func (i *ApiInstances) GetDowntimesApiV2() *datadogV2.DowntimesApi {
if i.downtimesApiV2 == nil {
i.downtimesApiV2 = datadogV2.NewDowntimesApi(i.HttpClient)
}
return i.downtimesApiV2
}

// GetDashboardListsApiV2 get instance of DashboardListsApi
func (i *ApiInstances) GetDashboardListsApiV2() *datadogV2.DashboardListsApi {
if i.dashboardListsApiV2 == nil {
Expand Down
38 changes: 38 additions & 0 deletions datadog/internal/validators/time_format_validator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package validators

import (
"context"
"fmt"
"time"

"github.com/hashicorp/terraform-plugin-framework/schema/validator"
)

type timeFormatValidator struct {
expectedFormat string
}

func (m timeFormatValidator) Description(context.Context) string {
return fmt.Sprintf("field is standardized to %v format", m.expectedFormat)
}

func (m timeFormatValidator) MarkdownDescription(ctx context.Context) string {
return m.Description(ctx)
}

func (m timeFormatValidator) ValidateString(ctx context.Context, req validator.StringRequest, resp *validator.StringResponse) {
if req.ConfigValue.IsNull() || req.ConfigValue.IsUnknown() {
return
}
if _, err := time.Parse(m.expectedFormat, req.ConfigValue.ValueString()); err != nil {
resp.Diagnostics.AddError(
fmt.Sprintf("property \"%s\" must be of the format %v", req.Path.String(), m.expectedFormat),
fmt.Sprintf("was %v", req.ConfigValue.ValueString()),
)
return
}
}

func TimeFormatValidator(expectedFormat string) validator.String {
return timeFormatValidator{expectedFormat}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2023-07-26T11:03:06.350353-07:00
110 changes: 110 additions & 0 deletions datadog/tests/cassettes/TestAccDowntimeScheduleBasicOneTime.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
---
version: 1
interactions:
- request:
body: |
{"data":{"attributes":{"display_timezone":"","monitor_identifier":{"monitor_tags":["cat:hat","mat:sat"]},"mute_first_recovery_notification":false,"notify_end_types":[],"schedule":{"end":null,"start":"2050-01-02T03:04:05Z"},"scope":"env:(staging OR tf-TestAccDowntimeScheduleBasicOneTime-local-1690394586)"},"type":"downtime"}}
form: {}
headers:
Accept:
- application/json
Content-Type:
- application/json
url: https://api.datadoghq.com/api/v2/downtime
method: POST
response:
body: |
{"data":{"type":"downtime","id":"ae02f456-2bde-11ee-83e1-da7ad0900002","attributes":{"canceled":null,"mute_first_recovery_notification":false,"monitor_identifier":{"monitor_tags":["cat:hat","mat:sat"]},"message":null,"display_timezone":"UTC","created":"2023-07-26T18:03:09.722405+00:00","notify_end_types":[],"modified":"2023-07-26T18:03:09.722405+00:00","scope":"env:(staging OR tf-TestAccDowntimeScheduleBasicOneTime-local-1690394586)","schedule":{"start":"2050-01-02T03:04:05+00:00","end":null},"notify_end_states":["alert","warn","no data"],"status":"scheduled"},"relationships":{"created_by":{"data":{"type":"users","id":"3ad549bf-eba0-11e9-a77a-0705486660d0"}},"monitor":{"data":null}}},"included":[{"type":"users","id":"3ad549bf-eba0-11e9-a77a-0705486660d0","attributes":{"name":null,"handle":"[email protected]","created_at":"2019-10-02T08:15:39.795051+00:00","modified_at":"2020-06-15T12:33:12.884459+00:00","email":"[email protected]","icon":"https://secure.gravatar.com/avatar/28a16dfe36e73b60c1d55872cb0f1172?s=48&d=retro","title":null,"verified":true,"service_account":false,"disabled":false,"allowed_login_methods":[],"status":"Active"}}]}
headers:
Content-Type:
- application/json
status: 200 OK
code: 200
duration: ""
- request:
body: ""
form: {}
headers:
Accept:
- application/json
url: https://api.datadoghq.com/api/v2/downtime/ae02f456-2bde-11ee-83e1-da7ad0900002
method: GET
response:
body: |
{"data":{"type":"downtime","id":"ae02f456-2bde-11ee-83e1-da7ad0900002","attributes":{"scope":"env:(staging OR tf-TestAccDowntimeScheduleBasicOneTime-local-1690394586)","created":"2023-07-26T18:03:09.722405+00:00","status":"scheduled","canceled":null,"modified":"2023-07-26T18:03:09.722405+00:00","notify_end_types":[],"notify_end_states":["warn","no data","alert"],"monitor_identifier":{"monitor_tags":["cat:hat","mat:sat"]},"display_timezone":"UTC","schedule":{"end":null,"start":"2050-01-02T03:04:05+00:00"},"mute_first_recovery_notification":false,"message":null}}}
headers:
Content-Type:
- application/json
status: 200 OK
code: 200
duration: ""
- request:
body: ""
form: {}
headers:
Accept:
- application/json
url: https://api.datadoghq.com/api/v2/downtime/ae02f456-2bde-11ee-83e1-da7ad0900002
method: GET
response:
body: |
{"data":{"type":"downtime","attributes":{"canceled":null,"scope":"env:(staging OR tf-TestAccDowntimeScheduleBasicOneTime-local-1690394586)","status":"scheduled","schedule":{"start":"2050-01-02T03:04:05+00:00","end":null},"modified":"2023-07-26T18:03:09.722405+00:00","mute_first_recovery_notification":false,"notify_end_types":[],"notify_end_states":["no data","alert","warn"],"created":"2023-07-26T18:03:09.722405+00:00","display_timezone":"UTC","message":null,"monitor_identifier":{"monitor_tags":["cat:hat","mat:sat"]}},"id":"ae02f456-2bde-11ee-83e1-da7ad0900002"}}
headers:
Content-Type:
- application/json
status: 200 OK
code: 200
duration: ""
- request:
body: |
{"data":{"attributes":{"display_timezone":"","message":"updated","monitor_identifier":{"monitor_tags":["vat:mat"]},"mute_first_recovery_notification":true,"notify_end_states":["alert"],"notify_end_types":["canceled"],"schedule":{"end":"2060-01-02T03:04:05Z","start":null},"scope":"env:(changed OR tf-TestAccDowntimeScheduleBasicOneTime-local-1690394586)"},"id":"","type":"downtime"}}
form: {}
headers:
Accept:
- application/json
Content-Type:
- application/json
url: https://api.datadoghq.com/api/v2/downtime/ae02f456-2bde-11ee-83e1-da7ad0900002
method: PATCH
response:
body: |
{"data":{"type":"downtime","attributes":{"schedule":{"start":"2023-07-26T18:03:11.486858+00:00","end":"2060-01-02T03:04:05+00:00"},"scope":"env:(changed OR tf-TestAccDowntimeScheduleBasicOneTime-local-1690394586)","canceled":null,"modified":"2023-07-26T18:03:11.529744+00:00","status":"active","notify_end_types":["canceled"],"monitor_identifier":{"monitor_tags":["vat:mat"]},"message":"updated","display_timezone":"UTC","notify_end_states":["alert"],"mute_first_recovery_notification":true,"created":"2023-07-26T18:03:09.722405+00:00"},"relationships":{"monitor":{"data":null},"created_by":{"data":{"type":"users","id":"3ad549bf-eba0-11e9-a77a-0705486660d0"}}},"id":"ae02f456-2bde-11ee-83e1-da7ad0900002"},"included":[{"type":"users","id":"3ad549bf-eba0-11e9-a77a-0705486660d0","attributes":{"name":null,"handle":"[email protected]","created_at":"2019-10-02T08:15:39.795051+00:00","modified_at":"2020-06-15T12:33:12.884459+00:00","email":"[email protected]","icon":"https://secure.gravatar.com/avatar/28a16dfe36e73b60c1d55872cb0f1172?s=48&d=retro","title":null,"verified":true,"service_account":false,"disabled":false,"allowed_login_methods":[],"status":"Active"}}]}
headers:
Content-Type:
- application/json
status: 200 OK
code: 200
duration: ""
- request:
body: ""
form: {}
headers:
Accept:
- application/json
url: https://api.datadoghq.com/api/v2/downtime/ae02f456-2bde-11ee-83e1-da7ad0900002
method: GET
response:
body: |
{"data":{"type":"downtime","attributes":{"message":"updated","modified":"2023-07-26T18:03:11.529744+00:00","monitor_identifier":{"monitor_tags":["vat:mat"]},"mute_first_recovery_notification":true,"schedule":{"end":"2060-01-02T03:04:05+00:00","start":"2023-07-26T18:03:11.486858+00:00"},"notify_end_states":["alert"],"created":"2023-07-26T18:03:09.722405+00:00","status":"active","display_timezone":"UTC","canceled":null,"notify_end_types":["canceled"],"scope":"env:(changed OR tf-TestAccDowntimeScheduleBasicOneTime-local-1690394586)"},"id":"ae02f456-2bde-11ee-83e1-da7ad0900002"}}
headers:
Content-Type:
- application/json
status: 200 OK
code: 200
duration: ""
- request:
body: ""
form: {}
headers:
Accept:
- '*/*'
url: https://api.datadoghq.com/api/v2/downtime/ae02f456-2bde-11ee-83e1-da7ad0900002
method: DELETE
response:
body: ""
headers:
Content-Type:
- text/html; charset=utf-8
status: 204 No Content
code: 204
duration: ""
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2023-07-26T09:56:03.253368-07:00
Loading