Skip to content

Commit

Permalink
-
Browse files Browse the repository at this point in the history
  • Loading branch information
tanmay-db committed Apr 11, 2024
1 parent c1e8a3d commit b670d4c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 44 deletions.
23 changes: 12 additions & 11 deletions clusters/resource_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,13 @@ func FixInstancePoolChangeIfAny(d *schema.ResourceData, cluster compute.CreateCl
}
}

type LibraryList struct {
Libraries []compute.Library `json:"libraries,omitempty" tf:"slice_set,alias:library"`
}

type ClusterSpec struct {
compute.ClusterSpec
LibraryList
}

func (ClusterSpec) CustomizeSchema(s map[string]*schema.Schema) map[string]*schema.Schema {
Expand Down Expand Up @@ -159,7 +164,6 @@ func (ClusterSpec) CustomizeSchema(s map[string]*schema.Schema) map[string]*sche
common.CustomizeSchemaPath(s, "cluster_log_conf", "dbfs", "destination").SetRequired()
common.CustomizeSchemaPath(s, "cluster_log_conf", "s3", "destination").SetRequired()
common.CustomizeSchemaPath(s, "spark_version").SetRequired()
common.CustomizeSchemaPath(s).AddNewField("library", common.StructToSchema(libraries.LibraryList{}, nil)["library"])
common.CustomizeSchemaPath(s).AddNewField("cluster_id", &schema.Schema{
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -240,13 +244,10 @@ func resourceClusterCreate(ctx context.Context, d *schema.ResourceData, c *commo
}
}

var libraryList libraries.LibraryList
var libraryList compute.InstallLibraries
common.DataToStructPointer(d, clusterSchema, &libraryList)
if len(libraryList.Libraries) > 0 {
if err = w.Libraries.Install(ctx, compute.InstallLibraries{
ClusterId: libraryList.ClusterId,
Libraries: libraryList.Libraries,
}); err != nil {
if err = w.Libraries.Install(ctx, libraryList); err != nil {
return err
}
_, err := libraries.WaitForLibrariesInstalledSdk(ctx, w, compute.Wait{
Expand Down Expand Up @@ -441,15 +442,15 @@ func resourceClusterUpdate(ctx context.Context, d *schema.ResourceData, c *commo
return err
}

var libraryList libraries.LibraryList
var libraryList LibraryList
common.DataToStructPointer(d, clusterSchema, &libraryList)
libsToInstall, libsToUninstall := libraries.GetLibrariesToInstallAndUninstall(libraryList, libsClusterStatus)
libsToInstall, libsToUninstall := libraries.GetLibrariesToInstallAndUninstall(libraryList.Libraries, libsClusterStatus)

clusterInfo, err = clusters.GetByClusterId(ctx, clusterId)
if err != nil {
return wrapMissingClusterError(err, d.Id())
}
if len(libsToUninstall.Libraries) > 0 || len(libsToInstall.Libraries) > 0 {
if len(libsToUninstall) > 0 || len(libsToInstall) > 0 {
if !clusterInfo.IsRunningOrResizing() {
if _, err = clusters.StartByClusterIdAndWait(ctx, clusterId); err != nil {
return err
Expand All @@ -459,8 +460,8 @@ func resourceClusterUpdate(ctx context.Context, d *schema.ResourceData, c *commo
// or errors out, so we just know the cluster is active.
err = w.Libraries.UpdateAndWait(ctx, compute.Update{
ClusterId: clusterId,
Install: libsToInstall.Libraries,
Uninstall: libsToUninstall.Libraries,
Install: libsToInstall,
Uninstall: libsToUninstall,
})
if err != nil {
return err
Expand Down
39 changes: 6 additions & 33 deletions libraries/libraries_api.go
Original file line number Diff line number Diff line change
@@ -1,41 +1,14 @@
package libraries

import (
"sort"

"github.com/databricks/databricks-sdk-go/service/compute"
"github.com/databricks/terraform-provider-databricks/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

type LibraryList struct {
// Unique identifier for the cluster on which to install these libraries.
ClusterId string `json:"cluster_id"`
// The libraries to install.
Libraries []compute.Library `json:"libraries,omitempty"`
}

func (l *LibraryList) Sort() {
sort.Slice(l.Libraries, func(i, j int) bool {
return l.Libraries[i].String() < l.Libraries[j].String()
})
}

func (l LibraryList) Aliases() map[string]map[string]string {
return map[string]map[string]string{
"libraries.LibraryList": {
"libraries": "library",
},
}
}

func (l LibraryList) CustomizeSchema(m map[string]*schema.Schema) map[string]*schema.Schema {
common.CustomizeSchemaPath(m, "library").Schema.Set = func(i any) int {
lib := NewLibraryFromInstanceState(i)
return schema.HashString(lib.String())
}
common.CustomizeSchemaPath(m, "library").SetSliceSet()
return m
Libraries []compute.Library `json:"libraries,omitempty" tf:"slice_set,alias:library"`
}

// NewLibraryFromInstanceState returns library from instance state for
Expand Down Expand Up @@ -77,18 +50,18 @@ func NewLibraryFromInstanceState(i any) (lib compute.Library) {
}

// Diff returns install/uninstall lists given a cluster lib status
func GetLibrariesToInstallAndUninstall(cll LibraryList, cls *compute.ClusterLibraryStatuses) (LibraryList, LibraryList) {
func GetLibrariesToInstallAndUninstall(cll []compute.Library, cls *compute.ClusterLibraryStatuses) ([]compute.Library, []compute.Library) {
inConfig := map[string]compute.Library{}
for _, lib := range cll.Libraries {
for _, lib := range cll {
inConfig[lib.String()] = lib
}
inState := map[string]compute.Library{}
for _, status := range cls.LibraryStatuses {
lib := *status.Library
inState[lib.String()] = lib
}
toInstall := LibraryList{ClusterId: cll.ClusterId}
toUninstall := LibraryList{ClusterId: cll.ClusterId}
toInstall := compute.InstallLibraries{}
toUninstall := compute.InstallLibraries{}
for key, lib := range inConfig {
_, exists := inState[key]
if exists {
Expand All @@ -105,5 +78,5 @@ func GetLibrariesToInstallAndUninstall(cll LibraryList, cls *compute.ClusterLibr
}
toInstall.Sort()
toUninstall.Sort()
return toInstall, toUninstall
return toInstall.Libraries, toUninstall.Libraries
}

0 comments on commit b670d4c

Please sign in to comment.