瀏覽代碼

Tutorial 8: import. Generation working.

Frédéric G. MARAND 1 年之前
父節點
當前提交
38f6cf6f44
共有 3 個文件被更改,包括 26 次插入6 次删除
  1. 6 0
      examples/order/imports.tf
  2. 8 4
      examples/order/main.tf
  3. 12 2
      internal/provider/order_resource.go

+ 6 - 0
examples/order/imports.tf

@@ -0,0 +1,6 @@
+# import {
+#   to = hashicups_order.edu
+#   id = "7"
+# }
+#
+#

+ 8 - 4
examples/order/main.tf

@@ -7,7 +7,7 @@ terraform {
 }
 
 provider "hashicups" {
-  host = "http://localhost:19090"
+  host     = "http://localhost:19090"
   username = "education"
   password = "test123"
 }
@@ -15,11 +15,15 @@ provider "hashicups" {
 resource "hashicups_order" "edu" {
   items = [
     {
-      coffee = { id = 3 }
+      coffee = {
+        id = 3
+      }
       quantity = 2
     },
     {
-      coffee = { id = 2 }
+      coffee = {
+        id = 2
+      }
       quantity = 2
     },
   ]
@@ -27,4 +31,4 @@ resource "hashicups_order" "edu" {
 
 output "edu_order" {
   value = hashicups_order.edu
-}
+}

+ 12 - 2
internal/provider/order_resource.go

@@ -7,6 +7,7 @@ import (
 	"time"
 
 	"github.com/hashicorp-demoapp/hashicups-client-go"
+	"github.com/hashicorp/terraform-plugin-framework/path"
 	"github.com/hashicorp/terraform-plugin-framework/resource"
 	"github.com/hashicorp/terraform-plugin-framework/resource/schema"
 	"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
@@ -16,8 +17,9 @@ import (
 
 // Ensure the implementation satisfies the expected interfaces.
 var (
-	_ resource.Resource              = &orderResource{}
-	_ resource.ResourceWithConfigure = &orderResource{}
+	_ resource.Resource                = &orderResource{}
+	_ resource.ResourceWithConfigure   = &orderResource{}
+	_ resource.ResourceWithImportState = &orderResource{}
 )
 
 // NewOrderResource is a helper function to simplify the provider implementation.
@@ -132,6 +134,7 @@ func (r *orderResource) Configure(_ context.Context, req resource.ConfigureReque
 
 // Create a new resource.
 func (r *orderResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
+	// Retrieve values from plan
 	var plan orderResourceModel
 
 	// 1. Checks whether the API Client is configured.
@@ -366,3 +369,10 @@ func (r *orderResource) Delete(ctx context.Context, req resource.DeleteRequest,
 		return
 	}
 }
+
+func (r *orderResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
+	// Retrieves import identifier and saves to attribute state.
+	// The method will use the resource.ImportStatePassthroughID() function
+	// to retrieve the ID value from the terraform import command and save it to the id attribute.
+	resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
+}

PANIC: session(release): write data/sessions/b/4/b49af5f62fb79459: no space left on device

PANIC

session(release): write data/sessions/b/4/b49af5f62fb79459: no space left on device
/my/cache/.heroku/go/go-path/pkg/mod/github.com/go-macaron/session@v1.0.3/session.go:204 (0xb13e07)
/my/cache/.heroku/go/go-path/pkg/mod/gopkg.in/macaron.v1@v1.5.1/context.go:80 (0x967b75)
/my/cache/.heroku/go/go-path/pkg/mod/github.com/go-macaron/inject@v0.0.0-20200308113650-138e5925c53b/inject.go:157 (0x9512ee)
/my/cache/.heroku/go/go-path/pkg/mod/github.com/go-macaron/inject@v0.0.0-20200308113650-138e5925c53b/inject.go:135 (0x951205)
/my/cache/.heroku/go/go-path/pkg/mod/gopkg.in/macaron.v1@v1.5.1/context.go:124 (0x967cc4)
/my/cache/.heroku/go/go-path/pkg/mod/gopkg.in/macaron.v1@v1.5.1/context.go:114 (0x967bf6)
/my/cache/.heroku/go/go-path/pkg/mod/gopkg.in/macaron.v1@v1.5.1/recovery.go:161 (0x15baec4)
/my/cache/.heroku/go/go-path/pkg/mod/gopkg.in/macaron.v1@v1.5.1/logger.go:40 (0x96b257)
/my/cache/.heroku/go/go-path/pkg/mod/github.com/go-macaron/inject@v0.0.0-20200308113650-138e5925c53b/inject.go:157 (0x9512ee)
/my/cache/.heroku/go/go-path/pkg/mod/github.com/go-macaron/inject@v0.0.0-20200308113650-138e5925c53b/inject.go:135 (0x951205)
/my/cache/.heroku/go/go-path/pkg/mod/gopkg.in/macaron.v1@v1.5.1/context.go:124 (0x967cc4)
/my/cache/.heroku/go/go-path/pkg/mod/gopkg.in/macaron.v1@v1.5.1/router.go:187 (0x972959)
/my/cache/.heroku/go/go-path/pkg/mod/gopkg.in/macaron.v1@v1.5.1/router.go:304 (0x973a01)
/my/cache/.heroku/go/go-path/pkg/mod/gopkg.in/macaron.v1@v1.5.1/macaron.go:218 (0x96c572)
/my/cache/.heroku/go/go1.26.3/go/src/net/http/server.go:3311 (0x85a5cd)
/my/cache/.heroku/go/go1.26.3/go/src/net/http/server.go:2073 (0x837f6f)
/my/cache/.heroku/go/go1.26.3/go/src/runtime/asm_amd64.s:1771 (0x493380)