Commit f8babc58 authored by Yann KAISER's avatar Yann KAISER

[BASE] First commit

parents
TEST?=./...
GOFMT_FILES?=$$(find . -name '*.go' | grep -v vendor)
default: test vet
tools:
go get -u github.com/kardianos/govendor
go get -u golang.org/x/tools/cmd/stringer
go get -u golang.org/x/tools/cmd/cover
# bin generates the releaseable binaries for Terraform
bin: fmtcheck generate
@TF_RELEASE=1 sh -c "'$(CURDIR)/scripts/build.sh'"
# dev creates binaries for testing Terraform locally. These are put
# into ./bin/ as well as $GOPATH/bin
dev: fmtcheck generate
@TF_DEV=1 sh -c "'$(CURDIR)/scripts/build.sh'"
quickdev: generate
@TF_DEV=1 sh -c "'$(CURDIR)/scripts/build.sh'"
# Shorthand for building and installing just one plugin for local testing.
# Run as (for example): make plugin-dev PLUGIN=provider-aws
plugin-dev: generate
go install github.com/hashicorp/terraform/builtin/bins/$(PLUGIN)
mv $(GOPATH)/bin/$(PLUGIN) $(GOPATH)/bin/terraform-$(PLUGIN)
# test runs the unit tests
# we run this one package at a time here because running the entire suite in
# one command creates memory usage issues when running in Travis-CI.
test: fmtcheck generate
go test -i $(TEST) || exit 1
go list $(TEST) | xargs -t -n4 go test $(TESTARGS) -timeout=60s -parallel=4
# testacc runs acceptance tests
testacc: fmtcheck generate
@if [ "$(TEST)" = "./..." ]; then \
echo "ERROR: Set TEST to a specific package. For example,"; \
echo " make testacc TEST=./builtin/providers/aws"; \
exit 1; \
fi
TF_ACC=1 go test $(TEST) -v $(TESTARGS) -timeout 120m
# e2etest runs the end-to-end tests against a generated Terraform binary
# The TF_ACC here allows network access, but does not require any special
# credentials since the e2etests use local-only providers such as "null".
e2etest: generate
TF_ACC=1 go test -v ./command/e2etest
test-compile: fmtcheck generate
@if [ "$(TEST)" = "./..." ]; then \
echo "ERROR: Set TEST to a specific package. For example,"; \
echo " make test-compile TEST=./builtin/providers/aws"; \
exit 1; \
fi
go test -c $(TEST) $(TESTARGS)
# testrace runs the race checker
testrace: fmtcheck generate
TF_ACC= go test -race $(TEST) $(TESTARGS)
cover:
@go tool cover 2>/dev/null; if [ $$? -eq 3 ]; then \
go get -u golang.org/x/tools/cmd/cover; \
fi
go test $(TEST) -coverprofile=coverage.out
go tool cover -html=coverage.out
rm coverage.out
# vet runs the Go source code static analysis tool `vet` to find
# any common errors.
vet:
@echo 'go vet ./...'
@go vet ./... ; if [ $$? -eq 1 ]; then \
echo ""; \
echo "Vet found suspicious constructs. Please check the reported constructs"; \
echo "and fix them if necessary before submitting the code for review."; \
exit 1; \
fi
# generate runs `go generate` to build the dynamically generated
# source files.
generate:
@which stringer > /dev/null; if [ $$? -ne 0 ]; then \
go get -u golang.org/x/tools/cmd/stringer; \
fi
go generate ./...
@go fmt command/internal_plugin_list.go > /dev/null
fmt:
gofmt -w $(GOFMT_FILES)
fmtcheck:
@sh -c "'$(CURDIR)/scripts/gofmtcheck.sh'"
vendor-status:
@govendor status
# disallow any parallelism (-j) for Make. This is necessary since some
# commands during the build process create temporary files that collide
# under parallel conditions.
.NOTPARALLEL:
.PHONY: bin cover default dev e2etest fmt fmtcheck generate plugin-dev quickdev test-compile test testacc testrace tools vendor-status vet
# Terraform Provider for Open Nebula
### Installation information
To build this project, you will also need to clone the opennebula-api repository.
Put both of the projets into your GO\_PATH and run go build into the provider project.
The opennebula-api does not need to be built separately.
#### **ROADMAP**
| Action | Done | Tested |
| --- | --- | --- |
| Create VM | Yes | Yes |
| Deploy VM | Yes | Yes |
| Rename VM | Yes | Yes |
| Resize VM | Yes | Yes |
| Chown VM | Yes | Yes |
| Multiple Disks on Creation | Yes | Yes |
| Disk-hotplug on VM | Yes | Yes |
| Network linking | Yes | Yes |
| Delete VM | Yes | Yes |
| User inputs in VM | Yes | Yes |
| Costs | Yes | Yes |
| --- | --- | --- |
| Create VM template | Yes | Yes |
| Template data source | Yes | Yes |
| Use VM template on VM creation | Yes | Yes |
| --- | --- | --- |
| Contextualisation | Yes | Yes |
| Variables / attributes | Yes | Yes |
| User inputs | Yes | Yes |
| --- | --- | --- |
| Service template creation | Yes | Yes |
| Service instanciation | Yes | Yes |
| --- | --- | --- |
| Resource mapping on creation | **Not entirely** | **Not entirely** |
| --- | --- | --- |
| Image creation | Yes | Yes |
| Image data source | Yes | Yes |
| Image deletion | Yes | Yes |
| --- | --- | --- |
| Virtual Network creation | Yes | Yes |
| Virtual Network deletion | Yes | Yes |
| --- | --- | --- |
| User creation | Yes | **No** |
| User data source | Yes | Yes |
| User deletion | Yes | **No** |
| Group creation | Yes | **No** |
| Group deletion | Yes | **No** |
| --- | --- | --- |
| Group VM creation | Yes | **No** |
| Group VM deletion | Yes | **No** |
Mapping on creation signifies that the resource shall not be created if it matches any other existing resource, depending on certain conditions, and that it will be mapped to that existing resource (it will have the same id). It is still in development.
Mapping conditions:
- VM: name OR any ip address
- Netork : name or base address
- Image : name
- VM Template : name
package main
import (
"github.com/hashicorp/terraform/plugin"
"github.com/hashicorp/terraform/terraform"
"terraform-provider-one/one"
)
func main() {
plugin.Serve(&plugin.ServeOpts{
ProviderFunc: func() terraform.ResourceProvider {
return one.Provider()
},
})
}
package one
import (
"opennebula-go/client"
"opennebula-go/image"
"opennebula-go/network"
"opennebula-go/oneflow"
"opennebula-go/system"
"opennebula-go/vm"
)
/*
Config struct
*/
type Config struct {
endpoint string
username string
password string
oneflowHost string
}
/*
Client struct
*/
type Client struct {
Session *client.Client
VMSession *vm.Client
ImageSession *image.Client
ServiceSession *oneflow.Client
NetworkSession *network.Client
SystemSession *system.Client
}
/*
Client method that creates a new client
*/
func (c *Config) Client() (interface{}, error) {
var oclient Client
nc, err := client.NewClient(c.endpoint, c.username, c.password)
if err != nil {
return nil, err
}
oclient.Session = nc
oclient.VMSession = vm.Session(nc)
oclient.ImageSession = image.Session(nc)
oclient.NetworkSession = network.Session(nc)
oclient.SystemSession = system.Session(nc)
oclient.ServiceSession = oneflow.Session(&client.OneflowClient{
Username: c.username,
Password: c.password,
Host: c.oneflowHost,
})
return &oclient, nil
}
package one
import (
"fmt"
"opennebula-go/image"
"strconv"
"github.com/hashicorp/terraform/helper/schema"
)
func dataSourceDatastore() *schema.Resource {
return &schema.Resource{
Read: dataSourceDatastoreRead,
Schema: map[string]*schema.Schema{
"id": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
},
}
}
func dataSourceDatastoreRead(d *schema.ResourceData, m interface{}) error {
session := m.(*Client).ImageSession
request := &image.Request{}
if v, ok := d.GetOk("id"); ok {
id := v.(int)
request.SpecificID = &id
}
if v, ok := d.GetOk("name"); ok {
name := v.(string)
request.SpecificName = &name
}
ds, err := session.FetchDatastore(request)
if err != nil {
return err
}
if ds == nil {
return fmt.Errorf("Unable to find a ds (by name: '" + *request.SpecificName + "' and id: '" + strconv.Itoa(*request.SpecificID) + "')")
}
d.SetId(strconv.Itoa(ds.ID))
return nil
}
package one
import (
"fmt"
"opennebula-go/vm"
"strconv"
"github.com/hashicorp/terraform/helper/schema"
)
func dataSourceHost() *schema.Resource {
return &schema.Resource{
Read: dataSourceHostRead,
Schema: map[string]*schema.Schema{
"strategy": &schema.Schema{ // name-id OR best-fit
Type: schema.TypeString,
Required: true,
},
"id": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
},
}
}
func dataSourceHostRead(d *schema.ResourceData, m interface{}) error {
session := m.(*Client).VMSession
strategy := d.Get("strategy").(string)
var host *vm.HostData
var err error
if strategy == "name-id" {
request := &vm.HostRequest{}
if v, ok := d.GetOk("id"); ok {
id := v.(int)
request.SpecificID = &id
}
if v, ok := d.GetOk("name"); ok {
name := v.(string)
request.SpecificName = &name
}
host, err = session.FetchHostByNameOrId(request)
if err != nil {
return err
}
} else if strategy == "best-fit" {
hostList, err := session.FetchHostList()
if err != nil {
return err
}
host = vm.BestHostSelection(hostList)
} else {
return fmt.Errorf("Bad strategy for host data source")
}
if host == nil {
return fmt.Errorf("Could not find a host")
}
d.SetId(strconv.Itoa(host.ID))
return nil
}
package one
import (
"fmt"
"opennebula-go/image"
"strconv"
"github.com/hashicorp/terraform/helper/schema"
)
func dataSourceImage() *schema.Resource {
return &schema.Resource{
Read: dataSourceImageRead,
Schema: map[string]*schema.Schema{
"id": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"image_filter": &schema.Schema{
Type: schema.TypeInt,
Default: -2,
Optional: true,
},
},
}
}
func dataSourceImageRead(d *schema.ResourceData, m interface{}) error {
session := m.(*Client).ImageSession
request := &image.Request{}
if v, ok := d.GetOk("id"); ok {
id := v.(int)
request.SpecificID = &id
}
if v, ok := d.GetOk("name"); ok {
name := v.(string)
request.SpecificName = &name
}
if v, ok := d.GetOk("image_filter"); ok {
imf := v.(int)
request.ImageFilter = &imf
}
image, err := session.FetchImage(request)
if err != nil {
return err
}
if image == nil {
return fmt.Errorf("Unable to find an image (by name: '" + *request.SpecificName + "' and id: '" + strconv.Itoa(*request.SpecificID) + "')")
}
d.SetId(strconv.Itoa(image.ID))
return nil
}
package one
import (
"opennebula-go/image"
"github.com/hashicorp/terraform/helper/schema"
)
func dataSourceService() *schema.Resource {
return &schema.Resource{
Read: dataSourceServiceRead,
Schema: map[string]*schema.Schema{
"id": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
},
}
}
func dataSourceServiceRead(d *schema.ResourceData, m interface{}) error {
//session := m.(*Client).ImageSession
request := &image.Request{}
if v, ok := d.GetOk("id"); ok {
id := v.(int)
request.SpecificID = &id
}
if v, ok := d.GetOk("name"); ok {
name := v.(string)
request.SpecificName = &name
}
if v, ok := d.GetOk("image_filter"); ok {
imf := v.(int)
request.ImageFilter = &imf
}
/*image, err := session.FetchImage(request)
if err != nil {
return err
}
d.SetId(strconv.Itoa(image.ID))
*/
return nil
}
package one
import (
"fmt"
"strconv"
"github.com/hashicorp/terraform/helper/schema"
)
func dataSourceUser() *schema.Resource {
return &schema.Resource{
Read: dataSourceUserRead,
Schema: map[string]*schema.Schema{
"id": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
},
"gid": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
},
"username": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
},
}
}
func dataSourceUserRead(d *schema.ResourceData, m interface{}) error {
session := m.(*Client).SystemSession
var username string
if v, ok := d.GetOk("username"); ok {
username = v.(string)
}
user, err := session.GetUserByUsername(username)
if err != nil {
return err
}
if user == nil {
return fmt.Errorf("Unable to find a user (by username: '" + username + "')")
}
d.Set("gid", user.GID)
d.SetId(strconv.Itoa(user.ID))
return nil
}
package one
import (
"fmt"
"strconv"
"github.com/hashicorp/terraform/helper/schema"
)
func dataSourceVMTemplate() *schema.Resource {
return &schema.Resource{
Read: dataSourceVMTemplateRead,
Schema: map[string]*schema.Schema{
"id": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"memory": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
},
"cpu": &schema.Schema{
Type: schema.TypeFloat,
Computed: true,
},
"vcpu": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
},
},
}
}
func dataSourceVMTemplateRead(d *schema.ResourceData, m interface{}) error {
session := m.(*Client).VMSession
var name string
var id = -1
if v, ok := d.GetOk("id"); ok {
id = v.(int)
}
if v, ok := d.GetOk("name"); ok {
name = v.(string)
}
vmData, err := session.CheckVMTemplateExistence(name, id)
if err != nil {
return err
}
if vmData == nil {
return fmt.Errorf("Unable to find a vm template (by name: '" + name + "' and id: '" + strconv.Itoa(id) + "')")
}
d.SetId(strconv.Itoa(vmData.ID))
d.Set("cpu", vmData.TemplateContent.CPUCount)
d.Set("vcpu", vmData.TemplateContent.VcpuCount)
d.Set("memory", vmData.TemplateContent.Memory)
return nil
}
package one
import (
"fmt"
"strconv"
"github.com/hashicorp/terraform/helper/schema"
)
func dataSourceVNetwork() *schema.Resource {
return &schema.Resource{
Read: dataSourceVNetworkRead,
Schema: map[string]*schema.Schema{
"id": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"address": &schema.Schema{
Type: schema.TypeString,