feat (builder/oracle-oci): add regex search on base image name

This commit is contained in:
js-g 2020-10-15 15:28:38 -04:00
parent 27128dc62f
commit 0c34d6ca12
3 changed files with 24 additions and 1 deletions

View File

@ -39,6 +39,7 @@ type ListImagesRequest struct {
// fields that can be specified under "base_image_filter" // fields that can be specified under "base_image_filter"
CompartmentId *string `mapstructure:"compartment_id"` CompartmentId *string `mapstructure:"compartment_id"`
DisplayName *string `mapstructure:"display_name"` DisplayName *string `mapstructure:"display_name"`
DisplayNameSearch *string `mapstructure:"display_name_search"`
OperatingSystem *string `mapstructure:"operating_system"` OperatingSystem *string `mapstructure:"operating_system"`
OperatingSystemVersion *string `mapstructure:"operating_system_version"` OperatingSystemVersion *string `mapstructure:"operating_system_version"`
Shape *string `mapstructure:"shape"` Shape *string `mapstructure:"shape"`

View File

@ -235,6 +235,7 @@ func (*FlatCreateVNICDetails) HCL2Spec() map[string]hcldec.Spec {
type FlatListImagesRequest struct { type FlatListImagesRequest struct {
CompartmentId *string `mapstructure:"compartment_id" cty:"compartment_id" hcl:"compartment_id"` CompartmentId *string `mapstructure:"compartment_id" cty:"compartment_id" hcl:"compartment_id"`
DisplayName *string `mapstructure:"display_name" cty:"display_name" hcl:"display_name"` DisplayName *string `mapstructure:"display_name" cty:"display_name" hcl:"display_name"`
DisplayNameSearch *string `mapstructure:"display_name_search" cty:"display_name_search" hcl:"display_name_search"`
OperatingSystem *string `mapstructure:"operating_system" cty:"operating_system" hcl:"operating_system"` OperatingSystem *string `mapstructure:"operating_system" cty:"operating_system" hcl:"operating_system"`
OperatingSystemVersion *string `mapstructure:"operating_system_version" cty:"operating_system_version" hcl:"operating_system_version"` OperatingSystemVersion *string `mapstructure:"operating_system_version" cty:"operating_system_version" hcl:"operating_system_version"`
Shape *string `mapstructure:"shape" cty:"shape" hcl:"shape"` Shape *string `mapstructure:"shape" cty:"shape" hcl:"shape"`
@ -254,6 +255,7 @@ func (*FlatListImagesRequest) HCL2Spec() map[string]hcldec.Spec {
s := map[string]hcldec.Spec{ s := map[string]hcldec.Spec{
"compartment_id": &hcldec.AttrSpec{Name: "compartment_id", Type: cty.String, Required: false}, "compartment_id": &hcldec.AttrSpec{Name: "compartment_id", Type: cty.String, Required: false},
"display_name": &hcldec.AttrSpec{Name: "display_name", Type: cty.String, Required: false}, "display_name": &hcldec.AttrSpec{Name: "display_name", Type: cty.String, Required: false},
"display_name_search": &hcldec.AttrSpec{Name: "display_name_search", Type: cty.String, Required: false},
"operating_system": &hcldec.AttrSpec{Name: "operating_system", Type: cty.String, Required: false}, "operating_system": &hcldec.AttrSpec{Name: "operating_system", Type: cty.String, Required: false},
"operating_system_version": &hcldec.AttrSpec{Name: "operating_system_version", Type: cty.String, Required: false}, "operating_system_version": &hcldec.AttrSpec{Name: "operating_system_version", Type: cty.String, Required: false},
"shape": &hcldec.AttrSpec{Name: "shape", Type: cty.String, Required: false}, "shape": &hcldec.AttrSpec{Name: "shape", Type: cty.String, Required: false},

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"regexp"
"time" "time"
core "github.com/oracle/oci-go-sdk/core" core "github.com/oracle/oci-go-sdk/core"
@ -69,6 +70,7 @@ func (d *driverOCI) CreateInstance(ctx context.Context, publicKey string) (strin
if d.cfg.BaseImageID != "" { if d.cfg.BaseImageID != "" {
imageId = &d.cfg.BaseImageID imageId = &d.cfg.BaseImageID
} else { } else {
// Pull images and determine which image ID to use, if BaseImageId not specified
response, err := d.computeClient.ListImages(ctx, core.ListImagesRequest{ response, err := d.computeClient.ListImages(ctx, core.ListImagesRequest{
CompartmentId: d.cfg.BaseImageFilter.CompartmentId, CompartmentId: d.cfg.BaseImageFilter.CompartmentId,
DisplayName: d.cfg.BaseImageFilter.DisplayName, DisplayName: d.cfg.BaseImageFilter.DisplayName,
@ -85,8 +87,26 @@ func (d *driverOCI) CreateInstance(ctx context.Context, publicKey string) (strin
if len(response.Items) == 0 { if len(response.Items) == 0 {
return "", errors.New("base_image_filter returned no images") return "", errors.New("base_image_filter returned no images")
} }
if d.cfg.BaseImageFilter.DisplayNameSearch != nil {
// Return most recent image that matches regex
imageNameRegex, err := regexp.Compile(*d.cfg.BaseImageFilter.DisplayNameSearch)
if err != nil {
return "", err
}
for _, image := range response.Items {
if imageNameRegex.MatchString(*image.DisplayName) {
imageId = image.Id
break
}
}
if imageId == nil {
return "", errors.New("No image matched display_name_search criteria")
}
} else {
// If no regex provided, simply return most recent image pulled
imageId = response.Items[0].Id imageId = response.Items[0].Id
} }
}
// Create Source details which will be used to Launch Instance // Create Source details which will be used to Launch Instance
InstanceSourceDetails := core.InstanceSourceViaImageDetails{ InstanceSourceDetails := core.InstanceSourceViaImageDetails{