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

WIP: Framework migration #443

Closed
wants to merge 9 commits into from
12 changes: 7 additions & 5 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-20230301133003-197f04efa071
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 @@ -52,6 +54,6 @@ require (
golang.org/x/text v0.7.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
)
21 changes: 12 additions & 9 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ github.com/hashicorp/go-hclog v1.2.1 h1:YQsLlGDJgwhXFpucSPyVbCBviQtjlHv3jLTlp8Ym
github.com/hashicorp/go-hclog v1.2.1/go.mod h1:W4Qnvbt70Wk/zYJryRzDRU/4r0kIg0PVHBcfoyhpF5M=
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
github.com/hashicorp/go-plugin v1.4.6 h1:MDV3UrKQBM3du3G7MApDGvOsMYy3JQJ4exhSoKBAeVA=
github.com/hashicorp/go-plugin v1.4.6/go.mod h1:viDMjcLJuDui6pXb8U4HVfb8AamCWhHGUjr2IrTF67s=
github.com/hashicorp/go-plugin v1.4.8 h1:CHGwpxYDOttQOY7HOWgETU9dyVjOXzniXDqJcYJE1zM=
github.com/hashicorp/go-plugin v1.4.8/go.mod h1:viDMjcLJuDui6pXb8U4HVfb8AamCWhHGUjr2IrTF67s=
github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/go-uuid v1.0.3 h1:2gKiV6YVmrJ1i2CKKa9obLvRieoRGviZFL26PcT/Co8=
github.com/hashicorp/go-uuid v1.0.3/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
Expand All @@ -99,14 +99,18 @@ github.com/hashicorp/terraform-exec v0.17.3 h1:MX14Kvnka/oWGmIkyuyvL6POx25ZmKrjl
github.com/hashicorp/terraform-exec v0.17.3/go.mod h1:+NELG0EqQekJzhvikkeQsOAZpsw0cv/03rbeQJqscAI=
github.com/hashicorp/terraform-json v0.14.0 h1:sh9iZ1Y8IFJLx+xQiKHGud6/TSUCM0N8e17dKDpqV7s=
github.com/hashicorp/terraform-json v0.14.0/go.mod h1:5A9HIWPkk4e5aeeXIBbkcOvaZbIYnAIkEyqP2pNSckM=
github.com/hashicorp/terraform-plugin-go v0.14.1 h1:cwZzPYla82XwAqpLhSzdVsOMU+6H29tczAwrB0z9Zek=
github.com/hashicorp/terraform-plugin-go v0.14.1/go.mod h1:Bc/K6K26BQ2FHqIELPbpKtt2CzzbQou+0UQF3/0NsCQ=
github.com/hashicorp/terraform-plugin-framework v1.1.1 h1:PbnEKHsIU8KTTzoztHQGgjZUWx7Kk8uGtpGMMc1p+oI=
github.com/hashicorp/terraform-plugin-framework v1.1.1/go.mod h1:DyZPxQA+4OKK5ELxFIIcqggcszqdWWUpTLPHAhS/tkY=
github.com/hashicorp/terraform-plugin-go v0.14.3 h1:nlnJ1GXKdMwsC8g1Nh05tK2wsC3+3BL/DBBxFEki+j0=
github.com/hashicorp/terraform-plugin-go v0.14.3/go.mod h1:7ees7DMZ263q8wQ6E4RdIdR6nHHJtrdt4ogX5lPkX1A=
github.com/hashicorp/terraform-plugin-log v0.7.0 h1:SDxJUyT8TwN4l5b5/VkiTIaQgY6R+Y2BQ0sRZftGKQs=
github.com/hashicorp/terraform-plugin-log v0.7.0/go.mod h1:p4R1jWBXRTvL4odmEkFfDdhUjHf9zcs/BCoNHAc7IK4=
github.com/hashicorp/terraform-plugin-mux v0.8.0 h1:WCTP66mZ+iIaIrCNJnjPEYnVjawTshnDJu12BcXK1EI=
github.com/hashicorp/terraform-plugin-mux v0.8.0/go.mod h1:vdW0daEi8Kd4RFJmet5Ot+SIVB/B8SwQVJiYKQwdCy8=
github.com/hashicorp/terraform-plugin-sdk/v2 v2.24.1 h1:zHcMbxY0+rFO9gY99elV/XC/UnQVg7FhRCbj1i5b7vM=
github.com/hashicorp/terraform-plugin-sdk/v2 v2.24.1/go.mod h1:+tNlb0wkfdsDJ7JEiERLz4HzM19HyiuIoGzTsM7rPpw=
github.com/hashicorp/terraform-registry-address v0.0.0-20220623143253-7d51757b572c h1:D8aRO6+mTqHfLsK/BC3j5OAoogv1WLRWzY1AaTo3rBg=
github.com/hashicorp/terraform-registry-address v0.0.0-20220623143253-7d51757b572c/go.mod h1:Wn3Na71knbXc1G8Lh+yu/dQWWJeFQEpDeJMtWMtlmNI=
github.com/hashicorp/terraform-registry-address v0.1.0 h1:W6JkV9wbum+m516rCl5/NjKxCyTVaaUBbzYcMzBDO3U=
github.com/hashicorp/terraform-registry-address v0.1.0/go.mod h1:EnyO2jYO6j29DTHbJcm00E5nQTFeTtyZH3H5ycydQ5A=
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734 h1:HKLsbzeOsfXmKNpr3GiT18XAblV0BjCbzL8KQAMZGa0=
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734/go.mod h1:kNDNcF7sN4DocDLBkQYz73HGKwN1ANB1blq4lIYLYvg=
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d h1:kJCB4vdITiW1eC1vq2e6IsrXKrZit1bv/TDYFGMp4BQ=
Expand Down Expand Up @@ -152,7 +156,6 @@ github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RR
github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ=
github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/nsf/jsondiff v0.0.0-20200515183724-f29ed568f4ce h1:RPclfga2SEJmgMmz2k+Mg7cowZ8yv4Trqw9UsJby758=
github.com/oklog/run v1.0.0 h1:Ru7dDtJNOyC66gQ5dQmaCa0qIsAUFY3sFpK1Xk8igrw=
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand Down Expand Up @@ -263,8 +266,8 @@ google.golang.org/genproto v0.0.0-20200711021454-869866162049/go.mod h1:FWY/as6D
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg=
google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk=
google.golang.org/grpc v1.50.1 h1:DS/BukOZWp8s6p4Dt/tOaJaTQyPyOoCcrjroHuCeLzY=
google.golang.org/grpc v1.50.1/go.mod h1:ZgQEeidpAuNRZ8iRrlBKXZQP1ghovWIVhdJRyCDK+GI=
google.golang.org/grpc v1.51.0 h1:E1eGv1FTqoLIdnBCZufiSHgKjlqG6fKFf6pPWtMTh8U=
google.golang.org/grpc v1.51.0/go.mod h1:wgNDFcnuBGmxLKI/qn4T+m5BtEBYXJPvibbUPsAIPww=
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
Expand Down
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