Skip to content

Instantly share code, notes, and snippets.

@junaid18183
Created January 4, 2017 11:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save junaid18183/750d267a9333764e26689389eb0f0f18 to your computer and use it in GitHub Desktop.
Save junaid18183/750d267a9333764e26689389eb0f0f18 to your computer and use it in GitHub Desktop.
Terraform custom Provider Template

This is a template to be used to create the dummy Terraform provider. For the example purpose I am using check_mk provider , the complete code for that can be found here - https://github.com/reancloud/terraform-provider-checkmk

Since this dummy check_mk provider has single resource named host, you have resource_host.go. Ideally you have to create a file named resource_.go for each resource your provider support

This dummy check_mk provider will work as {code} provider "checkmk" { user = "autouser" password = "UPFKWAJJDPJWTOQMOWHY" host = "192.168.99.100:32768" sitename = "mva" }

resource "checkmk_host" "winxp_1" { hostname = "winxp_1" folder = "os/windows" attribute_alias = "Alias of winxp_1" attribute_tag_agent = cmk-agent attribute_tag_criticality = "prod" attribute_ipaddress = "127.0.0.1" } {code}

package main
import (
"github.com/hashicorp/terraform/plugin"
)
func main() {
opts := plugin.ServeOpts{
ProviderFunc: Provider,
}
plugin.Serve(&opts)
}
package main
import (
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
)
func Provider() terraform.ResourceProvider {
return &schema.Provider{ // Source https://github.com/hashicorp/terraform/blob/v0.6.6/helper/schema/provider.go#L20-L43
Schema: providerSchema(),
ResourcesMap: map[string]*schema.Resource{
"checkmk_host": ResourceHost(),
},
ConfigureFunc: providerConfigure,
}
}
// List of supported configuration fields for your provider.
// Here we define a linked list of all the fields that we want to
// support in our provider (api_key, endpoint, timeout & max_retries).
func providerSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"user": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "Check_MK WebAPI username",
},
"password": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "Check_MK WebAPI password",
},
"host": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "Check_MK server host/ip port e.g. 192.168.99.100:32768",
},
"sitename": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "Check_MK sitename",
},
}
}
// This is the function used to fetch the configuration params given
// to our provider which we will use to initialise a dummy client that interacts with the API.
func providerConfigure(d *schema.ResourceData) (interface{}, error) {
client := ExampleClient{
User: d.Get("user").(string),
Password: d.Get("password").(string),
Host: d.Get("host").(string),
Sitename: d.Get("sitename").(string),
}
// You could have some field validations here, like checking that
// the API Key is has not expired or that the username/password
// combination is valid, etc.
return &client, nil
}
package main
import (
"github.com/hashicorp/terraform/helper/schema"
)
type ExampleClient struct {
User string
Password string
Host string
Sitename string
}
type Machine struct {
Hostname string
Folder string
Alias string
TAG_Agent string
TAG_criticality string
IPADDRESS string
}
func (m *Machine) Id() string {
return "id-" + m.Hostname + "!"
}
func (c *ExampleClient) CreateMachine(m *Machine) error {
return nil
}
// Here we define da linked list of all the resources that we want to
// support in our provider. As an example, if you were to write an AWS provider
// which supported resources like ec2 instances, elastic balancers and things of that sort
// then this would be the place to declare them.
func ResourceHost() *schema.Resource {
return &schema.Resource{
SchemaVersion: 1,
Create: createFunc,
Read: readFunc,
Update: updateFunc,
Delete: deleteFunc,
Schema: map[string]*schema.Schema{ // List of supported configuration fields for your resource
"hostname": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"folder": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"attribute_alias": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"attribute_tag_agent": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"attribute_tag_criticality": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"attribute_ipaddress": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
},
}
}
// The methods defined below will get called for each resource that needs to
// get created (createFunc), read (readFunc), updated (updateFunc) and deleted (deleteFunc).
// For example, if 10 resources need to be created then `createFunc`
// will get called 10 times every time with the information for the proper
// resource that is being mapped.
//
// If at some point any of these functions returns an error, Terraform will
// imply that something went wrong with the modification of the resource and it
// will prevent the execution of further calls that depend on that resource
// that failed to be created/updated/deleted.
func createFunc(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ExampleClient)
machine := Machine{
Hostname: d.Get("hostname").(string),
Folder: d.Get("folder").(string),
Alias: d.Get("attribute_alias").(string),
TAG_Agent: d.Get("attribute_tag_agent").(string),
TAG_criticality: d.Get("attribute_tag_criticality").(string),
IPADDRESS: d.Get("attribute_ipaddress").(string),
}
err := client.CreateMachine(&machine)
if err != nil {
return err
}
d.SetId(machine.Id())
return nil
}
func readFunc(d *schema.ResourceData, meta interface{}) error {
return nil
}
func updateFunc(d *schema.ResourceData, meta interface{}) error {
return nil
}
func deleteFunc(d *schema.ResourceData, meta interface{}) error {
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment