Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prepare framework migration #408

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ require (
github.com/OpenNebula/one/src/oca/go/src/goca v0.0.0-20221028091935-0c75c6db2132
github.com/hashicorp/go-uuid v1.0.3
github.com/hashicorp/go-version v1.6.0
github.com/hashicorp/terraform-plugin-framework v1.1.1
github.com/hashicorp/terraform-plugin-go v0.14.3
github.com/hashicorp/terraform-plugin-log v0.7.0
github.com/hashicorp/terraform-plugin-mux v0.8.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.24.1
)

Expand All @@ -22,15 +26,13 @@ require (
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320 // indirect
github.com/hashicorp/go-hclog v1.2.1 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-plugin v1.4.6 // indirect
github.com/hashicorp/go-plugin v1.4.8 // indirect
github.com/hashicorp/hc-install v0.4.0 // indirect
github.com/hashicorp/hcl/v2 v2.15.0 // indirect
github.com/hashicorp/logutils v1.0.0 // indirect
github.com/hashicorp/terraform-exec v0.17.3 // indirect
github.com/hashicorp/terraform-json v0.14.0 // indirect
github.com/hashicorp/terraform-plugin-go v0.14.1 // indirect
github.com/hashicorp/terraform-plugin-log v0.7.0 // indirect
github.com/hashicorp/terraform-registry-address v0.0.0-20220623143253-7d51757b572c // indirect
github.com/hashicorp/terraform-registry-address v0.1.0 // indirect
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734 // indirect
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d // indirect
github.com/kolo/xmlrpc v0.0.0-20190909154602-56d5ec7c422e // indirect
Expand All @@ -47,11 +49,11 @@ require (
github.com/vmihailenco/tagparser v0.1.1 // indirect
github.com/zclconf/go-cty v1.12.1 // indirect
golang.org/x/crypto v0.0.0-20220517005047-85d78b3ac167 // indirect
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 // indirect
golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
golang.org/x/text v0.4.0 // indirect
google.golang.org/appengine v1.6.6 // indirect
google.golang.org/genproto v0.0.0-20200711021454-869866162049 // indirect
google.golang.org/grpc v1.50.1 // indirect
google.golang.org/grpc v1.51.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
)
112 changes: 18 additions & 94 deletions go.sum

Large diffs are not rendered by default.

51 changes: 43 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,50 @@
package main

import (
"github.com/OpenNebula/terraform-provider-opennebula/opennebula"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/plugin"
"context"
"flag"
"log"

"github.com/hashicorp/terraform-plugin-framework/providerserver"
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
"github.com/hashicorp/terraform-plugin-go/tfprotov5/tf5server"
"github.com/hashicorp/terraform-plugin-mux/tf5muxserver"

provider "github.com/OpenNebula/terraform-provider-opennebula/opennebula"
providerFramework "github.com/OpenNebula/terraform-provider-opennebula/opennebula/framework"
)

func main() {
plugin.Serve(&plugin.ServeOpts{
ProviderFunc: func() *schema.Provider {
return opennebula.Provider()
},
})

ctx := context.Background()

var debug bool

flag.BoolVar(&debug, "debug", false, "set to true to run the provider with support for debuggers like delve")
flag.Parse()

providers := []func() tfprotov5.ProviderServer{
providerserver.NewProtocol5(providerFramework.New()),
provider.Provider().GRPCProvider,
}

muxServer, err := tf5muxserver.NewMuxServer(ctx, providers...)
if err != nil {
log.Fatal(err)
}

var serveOpts []tf5server.ServeOpt

if debug {
serveOpts = append(serveOpts, tf5server.WithManagedDebug())
}

err = tf5server.Serve(
"registry.terraform.io/OpenNebula/opennebula",
muxServer.ProviderServer,
serveOpts...,
)
if err != nil {
log.Fatal(err)
}
}
69 changes: 69 additions & 0 deletions opennebula/framework/common/tags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package common

import (
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

func TagsSchema() schema.Attribute {
return schema.MapAttribute{
Optional: true,
// MarkdownDescription: "",
Description: "Add custom tags to the resource",
ElementType: types.StringType,
}
}

type Tags struct {
Elements map[string]string
}

func (t *Tags) FromTerraform5Value(val tftypes.Value) error {

v := map[string]tftypes.Value{}
err := val.As(&v)
if err != nil {
return err
}

t.Elements = make(map[string]string)

for k, v := range v {
if v.Type().Is(tftypes.String) {
value := ""
v.As(&value)
t.Elements[k] = value
}
}

return nil
}

//type defaultTagsModifier struct {
// tags map[string]attr.Value
//}
//
//func (d defaultTagsModifier) Description(ctx context.Context) string {
// return fmt.Sprintf("Applies default tags then override with resource tags to produce the new plan")
//}
//
//func (d defaultTagsModifier) MarkdownDescription(ctx context.Context) string {
// return fmt.Sprintf("Applies default tags then override with resource tags to produce the new plan")
//
//}
//
//// PlanModifyString runs the logic of the plan modifier.
//// Access to the configuration, plan, and state is available in `req`, while
//// `resp` contains fields for updating the planned value, triggering resource
//// replacement, and returning diagnostics.
//func (d defaultTagsModifier) PlanModifyMap(ctx context.Context, req planmodifier.MapRequest, resp *planmodifier.MapResponse) {
// //req.Plan
//
// resp.PlanValue, resp.Diagnostics = types.MapValue(types.MapType{}, d.tags)
//}
//
//func defaultTagsModifierInit() *defaultTagsModifier {
// return &defaultTagsModifier{}
//}
//
69 changes: 69 additions & 0 deletions opennebula/framework/common/template_section.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package common

import (
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

func TemplateSectionBlock() schema.Block {
return schema.SetNestedBlock{
Description: "Add default tags to the resources",
NestedObject: schema.NestedBlockObject{
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
Optional: true,
Description: "Name of the section",
//MarkdownDescription: "",
},
"elements": schema.MapAttribute{
Optional: true,
Description: "Tags of the section",
ElementType: types.StringType,
//MarkdownDescription: "",
},
},
},
}
}

type TemplateSection struct {
Name string
Elements map[string]string
}

func (t *TemplateSection) FromTerraform5Value(val tftypes.Value) error {

// Get tags representation as golang types
v := map[string]tftypes.Value{}
err := val.As(&v)
if err != nil {
return err
}

// get name
err = v["name"].As(&t.Name)
if err != nil {
return err
}

// get section elements
tmpTags := make(map[string]tftypes.Value)

err = v["elements"].As(&tmpTags)
if err != nil {
return err
}

t.Elements = make(map[string]string)

for k, v := range tmpTags {
if v.Type().Is(tftypes.String) {
value := ""
v.As(&value)
t.Elements[k] = value
}
}

return nil
}
15 changes: 15 additions & 0 deletions opennebula/framework/config/provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package config

import (
"github.com/OpenNebula/one/src/oca/go/src/goca"
ver "github.com/hashicorp/go-version"

"github.com/OpenNebula/terraform-provider-opennebula/opennebula/framework/utils"
)

type Provider struct {
OneVersion *ver.Version
Controller *goca.Controller
Mutex utils.MutexKV
DefaultTags map[string]string
}
65 changes: 65 additions & 0 deletions opennebula/framework/default_tags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package opennebula

import (
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

type DefaultTags struct {
Elements map[string]string
}

func (t *DefaultTags) FromTerraform5Value(val tftypes.Value) error {

v := map[string]tftypes.Value{}
err := val.As(&v)
if err != nil {
return err
}

tmpTags := make(map[string]tftypes.Value)

err = v["tags"].As(&tmpTags)
if err != nil {
return err
}

t.Elements = make(map[string]string)

for k, v := range tmpTags {
if v.Type().Is(tftypes.String) {
value := ""
v.As(&value)
t.Elements[k] = value
}
}

return nil
}

//type defaultTagsModifier struct {
// tags map[string]attr.Value
//}
//
//func (d defaultTagsModifier) Description(ctx context.Context) string {
// return fmt.Sprintf("Applies default tags then override with resource tags to produce the new plan")
//}
//
//func (d defaultTagsModifier) MarkdownDescription(ctx context.Context) string {
// return fmt.Sprintf("Applies default tags then override with resource tags to produce the new plan")
//
//}
//
//// PlanModifyString runs the logic of the plan modifier.
//// Access to the configuration, plan, and state is available in `req`, while
//// `resp` contains fields for updating the planned value, triggering resource
//// replacement, and returning diagnostics.
//func (d defaultTagsModifier) PlanModifyMap(ctx context.Context, req planmodifier.MapRequest, resp *planmodifier.MapResponse) {
// //req.Plan
//
// resp.PlanValue, resp.Diagnostics = types.MapValue(types.MapType{}, d.tags)
//}
//
//func defaultTagsModifierInit() *defaultTagsModifier {
// return &defaultTagsModifier{}
//}
//
Loading