-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresources.go
171 lines (155 loc) · 6.3 KB
/
resources.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Copyright 2016-2018, Pulumi Corporation.
//
// 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 keycloak
import (
"unicode"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"github.com/mrparkers/terraform-provider-keycloak/provider"
"github.com/pulumi/pulumi-terraform-bridge/pkg/tfbridge"
"github.com/pulumi/pulumi/pkg/resource"
"github.com/pulumi/pulumi/pkg/tokens"
)
// all of the token components used below.
const (
// packages:
mainPkg = "keycloak"
// modules:
mainMod = "index" // the y module
)
// makeMember manufactures a type token for the package and the given module and type.
func makeMember(mod string, mem string) tokens.ModuleMember {
return tokens.ModuleMember(mainPkg + ":" + mod + ":" + mem)
}
// makeType manufactures a type token for the package and the given module and type.
func makeType(mod string, typ string) tokens.Type {
return tokens.Type(makeMember(mod, typ))
}
// makeDataSource manufactures a standard resource token given a module and resource name. It
// automatically uses the main package and names the file by simply lower casing the data source's
// first character.
func makeDataSource(mod string, res string) tokens.ModuleMember {
fn := string(unicode.ToLower(rune(res[0]))) + res[1:]
return makeMember(mod+"/"+fn, res)
}
// makeResource manufactures a standard resource token given a module and resource name. It
// automatically uses the main package and names the file by simply lower casing the resource's
// first character.
func makeResource(mod string, res string) tokens.Type {
fn := string(unicode.ToLower(rune(res[0]))) + res[1:]
return makeType(mod+"/"+fn, res)
}
// boolRef returns a reference to the bool argument.
func boolRef(b bool) *bool {
return &b
}
// stringValue gets a string value from a property map if present, else ""
func stringValue(vars resource.PropertyMap, prop resource.PropertyKey) string {
val, ok := vars[prop]
if ok && val.IsString() {
return val.StringValue()
}
return ""
}
// preConfigureCallback is called before the providerConfigure function of the underlying provider.
// It should validate that the provider can be configured, and provide actionable errors in the case
// it cannot be. Configuration variables can be read from `vars` using the `stringValue` function -
// for example `stringValue(vars, "accessKey")`.
func preConfigureCallback(vars resource.PropertyMap, c *terraform.ResourceConfig) error {
return nil
}
// managedByPulumi is a default used for some managed resources, in the absence of something more meaningful.
var managedByPulumi = &tfbridge.DefaultInfo{Value: "Managed by Pulumi"}
// Provider returns additional overlaid schema and metadata associated with the provider..
func Provider() tfbridge.ProviderInfo {
// Instantiate the Terraform provider
p := provider.KeycloakProvider()
// Create a Pulumi provider mapping
prov := tfbridge.ProviderInfo{
P: p,
Name: "keycloak",
Description: "A Pulumi package for creating and managing keycloak cloud resources.",
Keywords: []string{"pulumi", "keycloak"},
License: "Apache-2.0",
Homepage: "https://pulumi.io",
Repository: "https://github.com/pulumi/pulumi-keycloak",
Config: map[string]*tfbridge.SchemaInfo{
// Add any required configuration here, or remove the example below if
// no additional points are required.
// "region": {
// Type: makeType("region", "Region"),
// Default: &tfbridge.DefaultInfo{
// EnvVars: []string{"AWS_REGION", "AWS_DEFAULT_REGION"},
// },
// },
},
PreConfigureCallback: preConfigureCallback,
Resources: map[string]*tfbridge.ResourceInfo{
// Map each resource in the Terraform provider to a Pulumi type. Two examples
// are below - the single line form is the common case. The multi-line form is
// needed only if you wish to override types or other default options.
//
// "aws_iam_role": {Tok: makeResource(mainMod, "IamRole")}
//
// "aws_acm_certificate": {
// Tok: makeResource(mainMod, "Certificate"),
// Fields: map[string]*tfbridge.SchemaInfo{
// "tags": {Type: makeType(mainPkg, "Tags")},
// },
// },
},
DataSources: map[string]*tfbridge.DataSourceInfo{
// Map each resource in the Terraform provider to a Pulumi function. An example
// is below.
// "aws_ami": {Tok: makeDataSource(mainMod, "getAmi")},
},
JavaScript: &tfbridge.JavaScriptInfo{
// List any npm dependencies and their versions
Dependencies: map[string]string{
"@pulumi/pulumi": "latest",
},
DevDependencies: map[string]string{
"@types/node": "^8.0.25", // so we can access strongly typed node definitions.
"@types/mime": "^2.0.0",
},
// See the documentation for tfbridge.OverlayInfo for how to lay out this
// section, or refer to the AWS provider. Delete this section if there are
// no overlay files.
//Overlay: &tfbridge.OverlayInfo{},
},
Python: &tfbridge.PythonInfo{
// List any Python dependencies and their version ranges
Requires: map[string]string{
"pulumi": ">=1.0.0,<2.0.0",
},
},
}
// For all resources with name properties, we will add an auto-name property. Make sure to skip those that
// already have a name mapping entry, since those may have custom overrides set above (e.g., for length).
const nameProperty = "name"
for resname, res := range prov.Resources {
if schema := p.ResourcesMap[resname]; schema != nil {
// Only apply auto-name to input properties (Optional || Required) named `name`
if tfs, has := schema.Schema[nameProperty]; has && (tfs.Optional || tfs.Required) {
if _, hasfield := res.Fields[nameProperty]; !hasfield {
if res.Fields == nil {
res.Fields = make(map[string]*tfbridge.SchemaInfo)
}
res.Fields[nameProperty] = tfbridge.AutoName(nameProperty, 255)
}
}
}
}
return prov
}