diff --git a/builder/gridscale/artifact.go b/builder/gridscale/artifact.go index 6be0469..5f94900 100644 --- a/builder/gridscale/artifact.go +++ b/builder/gridscale/artifact.go @@ -3,8 +3,10 @@ package gridscale import ( "context" "fmt" - "github.com/gridscale/gsclient-go/v3" "log" + + "github.com/gridscale/gsclient-go/v3" + registryimage "github.com/hashicorp/packer-plugin-sdk/packer/registry/image" ) type Artifact struct { @@ -14,8 +16,18 @@ type Artifact struct { // The UUID of the template TemplateUUID string + // Location name. + LocationName string + + // Location UUID. + LocationUUID string + // The client for making API calls Client gsclient.TemplateOperator + + // StateData should store data such as GeneratedData + // to be shared with post-processors + StateData map[string]interface{} } func (*Artifact) BuilderId() string { @@ -36,7 +48,20 @@ func (a *Artifact) String() string { } func (a *Artifact) State(name string) interface{} { - return nil + if name == registryimage.ArtifactStateURI { + img, err := registryimage.FromArtifact(a, + registryimage.WithID(a.TemplateName), + registryimage.WithProvider("gridscale"), + registryimage.WithSourceID(a.TemplateUUID), + registryimage.WithRegion(a.LocationName), + ) + if err != nil { + log.Printf("[DEBUG] error encountered when creating a registry image %v", err) + return nil + } + return img + } + return a.StateData[name] } func (a *Artifact) Destroy() error { diff --git a/builder/gridscale/builder.go b/builder/gridscale/builder.go index 3957b1f..8cf1220 100644 --- a/builder/gridscale/builder.go +++ b/builder/gridscale/builder.go @@ -189,6 +189,8 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack artifact := &Artifact{ TemplateName: b.config.TemplateName, TemplateUUID: state.Get("template_uuid").(string), + LocationName: state.Get("location_name").(string), + LocationUUID: state.Get("location_uuid").(string), Client: client, } diff --git a/builder/gridscale/step_create_template.go b/builder/gridscale/step_create_template.go index a7a63a1..531bc30 100644 --- a/builder/gridscale/step_create_template.go +++ b/builder/gridscale/step_create_template.go @@ -37,7 +37,7 @@ func (s *stepCreateTemplate) Run(ctx context.Context, state multistep.StateBag) return multistep.ActionHalt } ui.Say(fmt.Sprintf("Creating template: %v", c.TemplateName)) - template, err := client.CreateTemplate( + templateRes, err := client.CreateTemplate( context.Background(), gsclient.TemplateCreateRequest{ Name: c.TemplateName, @@ -50,8 +50,20 @@ func (s *stepCreateTemplate) Run(ctx context.Context, state multistep.StateBag) ui.Error(err.Error()) return multistep.ActionHalt } - state.Put("template_uuid", template.ObjectUUID) - ui.Say(fmt.Sprintf("Created template %v with uuid: %v", c.TemplateName, template.ObjectUUID)) + template, err := client.GetTemplate( + context.Background(), + templateRes.ObjectUUID, + ) + if err != nil { + err := fmt.Errorf("Error getting template: %s", err) + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt + } + state.Put("template_uuid", template.Properties.ObjectUUID) + state.Put("location_name", template.Properties.LocationName) + state.Put("location_uuid", template.Properties.LocationUUID) + ui.Say(fmt.Sprintf("Created template %v with uuid: %v", c.TemplateName, template.Properties.ObjectUUID)) return multistep.ActionContinue } diff --git a/docs/builders/gridscale.mdx b/docs/builders/gridscale.mdx index b1b1df9..475f076 100644 --- a/docs/builders/gridscale.mdx +++ b/docs/builders/gridscale.mdx @@ -27,7 +27,7 @@ packer { required_plugins { gridscale = { version = ">= 0.0.1" - source = "github.com/hashicorp/gridscale" + source = "github.com/gridscale/gridscale" } } }