/
maintenance.go
132 lines (114 loc) · 4.58 KB
/
maintenance.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
* @license
* Copyright 2020 Dynatrace LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package provider
import (
"context"
"reflect"
api "github.com/dtcookie/dynatrace/api/config"
"github.com/dtcookie/dynatrace/api/config/maintenancewindows"
"github.com/dtcookie/dynatrace/rest"
"github.com/dtcookie/dynatrace/terraform"
"github.com/dtcookie/opt"
"github.com/dynatrace-oss/terraform-provider-dynatrace/config"
"github.com/dynatrace-oss/terraform-provider-dynatrace/logging"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
type maintWins struct {
}
// Resource produces the schema definition for Maintenance Windows
func (mzs *maintWins) Resource() *schema.Resource {
resource := terraform.ResourceFor(new(maintenancewindows.MaintenanceWindow))
resource.CreateContext = logging.Enable(mzs.Create)
resource.UpdateContext = logging.Enable(mzs.Update)
resource.ReadContext = logging.Enable(mzs.Read)
resource.DeleteContext = logging.Enable(mzs.Delete)
return resource
}
// Create expects the configuration of a Maintenance Window within the given ResourceData
// and sends them to the Dynatrace Server in order to create that resource
func (mzs *maintWins) Create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
rest.Verbose = config.HTTPVerbose
var err error
var resolver terraform.Resolver
if resolver, err = terraform.NewResolver(d); err != nil {
return diag.FromErr(err)
}
var untypedInstance interface{}
if untypedInstance, err = resolver.Resolve(reflect.TypeOf(maintenancewindows.MaintenanceWindow{})); err != nil {
return diag.FromErr(err)
}
instance := untypedInstance.(maintenancewindows.MaintenanceWindow)
instance.ID = nil
conf := m.(*config.ProviderConfiguration)
apiService := maintenancewindows.NewService(conf.DTenvURL, conf.APIToken)
var stub *api.EntityShortRepresentation
if stub, err = apiService.Create(&instance); err != nil {
return diag.FromErr(err)
}
d.SetId(stub.ID)
return mzs.Read(ctx, d, m)
}
// Update expects the configuration of a Maintenance Window within the given ResourceData
// and sends them to the Dynatrace Server in order to modify that resource
func (mzs *maintWins) Update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
rest.Verbose = config.HTTPVerbose
var err error
var resolver terraform.Resolver
if resolver, err = terraform.NewResolver(d); err != nil {
return diag.FromErr(err)
}
var untypedInstance interface{}
if untypedInstance, err = resolver.Resolve(reflect.TypeOf(maintenancewindows.MaintenanceWindow{})); err != nil {
return diag.FromErr(err)
}
instance := untypedInstance.(maintenancewindows.MaintenanceWindow)
instance.ID = opt.NewString(d.Id())
conf := m.(*config.ProviderConfiguration)
apiService := maintenancewindows.NewService(conf.DTenvURL, conf.APIToken)
if err = apiService.Update(&instance); err != nil {
return diag.FromErr(err)
}
return mzs.Read(ctx, d, m)
}
// Read queries the Dynatrace REST API for the configuration of a Maintenance Window
// defined by the ID contained within the given ResourceData
func (mzs *maintWins) Read(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
rest.Verbose = config.HTTPVerbose
var err error
conf := m.(*config.ProviderConfiguration)
apiService := maintenancewindows.NewService(conf.DTenvURL, conf.APIToken)
var instance *maintenancewindows.MaintenanceWindow
if instance, err = apiService.Get(d.Id()); err != nil {
return diag.FromErr(err)
}
if err = terraform.ToTerraform(instance, d); err != nil {
return diag.FromErr(err)
}
return diag.Diagnostics{}
}
// Delete deletes the configuration of a Maintenance Window from the Dynatrace Server
func (mzs *maintWins) Delete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
rest.Verbose = config.HTTPVerbose
var err error
conf := m.(*config.ProviderConfiguration)
apiService := maintenancewindows.NewService(conf.DTenvURL, conf.APIToken)
if err = apiService.Delete(d.Id()); err != nil {
return diag.FromErr(err)
}
return diag.Diagnostics{}
}