forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blueprint_definition_data_source.go
120 lines (96 loc) · 2.96 KB
/
blueprint_definition_data_source.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
package blueprints
import (
"fmt"
"time"
"github.com/kevinklinger/terraform-provider-azurerm/v2/helpers/azure"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/clients"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/services/blueprints/validate"
mgValidate "github.com/kevinklinger/terraform-provider-azurerm/v2/internal/services/managementgroup/validate"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/tf/pluginsdk"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/tf/validation"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/timeouts"
"github.com/kevinklinger/terraform-provider-azurerm/v2/utils"
)
func dataSourceBlueprintDefinition() *pluginsdk.Resource {
return &pluginsdk.Resource{
Read: dataSourceBlueprintDefinitionRead,
Timeouts: &pluginsdk.ResourceTimeout{
Read: pluginsdk.DefaultTimeout(5 * time.Minute),
},
Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validate.DefinitionName,
},
"scope_id": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.Any(
azure.ValidateResourceID,
mgValidate.ManagementGroupID,
),
},
// Computed
"description": {
Type: pluginsdk.TypeString,
Computed: true,
},
"display_name": {
Type: pluginsdk.TypeString,
Computed: true,
},
"last_modified": {
Type: pluginsdk.TypeString,
Computed: true,
},
"target_scope": {
Type: pluginsdk.TypeString,
Computed: true,
},
"time_created": {
Type: pluginsdk.TypeString,
Computed: true,
},
"versions": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},
},
}
}
func dataSourceBlueprintDefinitionRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Blueprints.BlueprintsClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()
name := d.Get("name").(string)
scope := d.Get("scope_id").(string)
resp, err := client.Get(ctx, scope, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Blueprint Definition %q not found in Scope (%q): %+v", name, scope, err)
}
return fmt.Errorf("Read failed for Blueprint Definition (%q) in Sccope (%q): %+v", name, scope, err)
}
if resp.ID == nil || *resp.ID == "" {
return fmt.Errorf("Failed to retrieve ID for Blueprint %q", name)
} else {
d.SetId(*resp.ID)
}
if resp.Description != nil {
d.Set("description", resp.Description)
}
if resp.DisplayName != nil {
d.Set("display_name", resp.DisplayName)
}
d.Set("last_modified", resp.Status.LastModified.String())
d.Set("time_created", resp.Status.TimeCreated.String())
d.Set("target_scope", resp.TargetScope)
if resp.Versions != nil {
d.Set("versions", resp.Versions)
}
return nil
}