/
resource_collection.go
90 lines (78 loc) · 2.9 KB
/
resource_collection.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
// Copyright 2024 Chronosphere Inc.
//
// 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 chronosphere
import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"go.uber.org/atomic"
"github.com/chronosphereio/terraform-provider-chronosphere/chronosphere/intschema"
"github.com/chronosphereio/terraform-provider-chronosphere/chronosphere/pkg/configv1/models"
"github.com/chronosphereio/terraform-provider-chronosphere/chronosphere/pkg/localid"
"github.com/chronosphereio/terraform-provider-chronosphere/chronosphere/tfid"
"github.com/chronosphereio/terraform-provider-chronosphere/chronosphere/tfschema"
)
// CollectionFromModel maps an API model to the intschema model.
func CollectionFromModel(m *models.Configv1Collection) (*intschema.Collection, error) {
return (collectionConverter{}).fromModel(m)
}
func resourceCollection() *schema.Resource {
r := newGenericResource[
*models.Configv1Collection,
intschema.Collection,
*intschema.Collection,
](
"collection",
collectionConverter{},
generatedCollection{},
)
return &schema.Resource{
CreateContext: r.CreateContext,
ReadContext: r.ReadContext,
UpdateContext: r.UpdateContext,
DeleteContext: r.DeleteContext,
Schema: tfschema.Collection,
CustomizeDiff: r.ValidateDryRun(&CollectionDryRunCount),
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
}
}
// CollectionDryRunCount tracks how many times dry run is run during validation for testing.
var CollectionDryRunCount atomic.Int64
type collectionConverter struct{}
func (collectionConverter) toModel(
c *intschema.Collection,
) (*models.Configv1Collection, error) {
if localid.IsLocalID(c.NotificationPolicyId.Slug()) {
return nil, fmt.Errorf("notification_policy_id must reference a notification policy with name")
}
return &models.Configv1Collection{
Slug: c.Slug,
Name: c.Name,
NotificationPolicySlug: c.NotificationPolicyId.Slug(),
TeamSlug: c.TeamId.Slug(),
Description: c.Description,
}, nil
}
func (collectionConverter) fromModel(
m *models.Configv1Collection,
) (*intschema.Collection, error) {
return &intschema.Collection{
Slug: m.Slug,
Name: m.Name,
NotificationPolicyId: tfid.Slug(m.NotificationPolicySlug),
Description: m.Description,
TeamId: tfid.Slug(m.TeamSlug),
}, nil
}