diff --git a/clusters/resource_cluster.go b/clusters/resource_cluster.go index 3c0350202..a6100ae07 100644 --- a/clusters/resource_cluster.go +++ b/clusters/resource_cluster.go @@ -513,20 +513,23 @@ func resourceClusterCreate(ctx context.Context, d *schema.ResourceData, c *commo } func setPinnedStatus(ctx context.Context, d *schema.ResourceData, clusterAPI compute.ClustersInterface) error { - events, err := clusterAPI.EventsAll(ctx, compute.GetEvents{ - ClusterId: d.Id(), - Limit: 1, - Order: compute.GetEventsOrderDesc, - EventTypes: []compute.EventType{compute.EventTypePinned, compute.EventTypeUnpinned}, + clusterDetails := clusterAPI.List(ctx, compute.ListClustersRequest{ + FilterBy: &compute.ListClustersFilterBy{ + IsPinned: true, + }, + PageSize: 100, // pinned cluster limit - just get all of them }) - if err != nil { - return err - } - pinnedEvent := compute.EventTypeUnpinned - if len(events) > 0 { - pinnedEvent = events[0].Type + + for clusterDetails.HasNext(ctx) { + detail, err := clusterDetails.Next(ctx) + if err != nil { + return err + } + if detail.ClusterId == d.Id() { + return d.Set("is_pinned", true) + } } - return d.Set("is_pinned", pinnedEvent == compute.EventTypePinned) + return d.Set("is_pinned", false) } func resourceClusterRead(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error { diff --git a/clusters/resource_cluster_test.go b/clusters/resource_cluster_test.go index 240b62cb4..a15633de5 100644 --- a/clusters/resource_cluster_test.go +++ b/clusters/resource_cluster_test.go @@ -45,17 +45,10 @@ func TestResourceClusterCreate(t *testing.T) { }, }, { - Method: "POST", - Resource: "/api/2.1/clusters/events", - ExpectedRequest: compute.GetEvents{ - ClusterId: "abc", - Limit: 1, - Order: compute.GetEventsOrderDesc, - EventTypes: []compute.EventType{compute.EventTypePinned, compute.EventTypeUnpinned}, - }, - Response: compute.GetEventsResponse{ - Events: []compute.ClusterEvent{}, - TotalCount: 0, + Method: "GET", + Resource: "/api/2.1/clusters/list?filter_by.is_pinned=true&page_size=100", + Response: compute.ListClustersResponse{ + Clusters: []compute.ClusterDetails{}, }, }, { @@ -79,6 +72,7 @@ func TestResourceClusterCreate(t *testing.T) { }.Apply(t) assert.NoError(t, err) assert.Equal(t, "abc", d.Id()) + assert.Equal(t, false, d.Get("is_pinned")) } func TestResourceClusterCreatePinned(t *testing.T) { @@ -128,24 +122,18 @@ func TestResourceClusterCreatePinned(t *testing.T) { }, }, { - Method: "POST", - Resource: "/api/2.1/clusters/events", - ExpectedRequest: compute.GetEvents{ - ClusterId: "abc", - Limit: 1, - Order: compute.GetEventsOrderDesc, - EventTypes: []compute.EventType{compute.EventTypePinned, compute.EventTypeUnpinned}, - }, - Response: compute.GetEventsResponse{ - Events: []compute.ClusterEvent{ - { - ClusterId: "abc", - Timestamp: int64(123), - Type: compute.EventTypePinned, - Details: &compute.EventDetails{}, - }, - }, - TotalCount: 1, + Method: "GET", + Resource: "/api/2.1/clusters/list?filter_by.is_pinned=true&page_size=100", + Response: compute.ListClustersResponse{ + Clusters: []compute.ClusterDetails{{ + ClusterId: "abc", + NumWorkers: 100, + ClusterName: "Shared Autoscaling", + SparkVersion: "7.1-scala12", + NodeTypeId: "i3.xlarge", + AutoterminationMinutes: 15, + State: compute.StateRunning, + }}, }, }, }, @@ -162,6 +150,7 @@ func TestResourceClusterCreatePinned(t *testing.T) { }.Apply(t) assert.NoError(t, err) assert.Equal(t, "abc", d.Id()) + assert.Equal(t, true, d.Get("is_pinned")) } func TestResourceClusterCreateErrorFollowedByDeletion(t *testing.T) { @@ -307,17 +296,10 @@ func TestResourceClusterCreate_WithLibraries(t *testing.T) { }, }, { - Method: "POST", - Resource: "/api/2.1/clusters/events", - ExpectedRequest: compute.GetEvents{ - ClusterId: "abc", - Limit: 1, - Order: compute.GetEventsOrderDesc, - EventTypes: []compute.EventType{compute.EventTypePinned, compute.EventTypeUnpinned}, - }, - Response: compute.GetEventsResponse{ - Events: []compute.ClusterEvent{}, - TotalCount: 0, + Method: "GET", + Resource: "/api/2.1/clusters/list?filter_by.is_pinned=true&page_size=100", + Response: compute.ListClustersResponse{ + Clusters: []compute.ClusterDetails{}, }, }, { @@ -506,17 +488,10 @@ func TestResourceClusterCreatePhoton(t *testing.T) { }, }, { - Method: "POST", - Resource: "/api/2.1/clusters/events", - ExpectedRequest: compute.GetEvents{ - ClusterId: "abc", - Limit: 1, - Order: compute.GetEventsOrderDesc, - EventTypes: []compute.EventType{compute.EventTypePinned, compute.EventTypeUnpinned}, - }, - Response: compute.GetEventsResponse{ - Events: []compute.ClusterEvent{}, - TotalCount: 0, + Method: "GET", + Resource: "/api/2.1/clusters/list?filter_by.is_pinned=true&page_size=100", + Response: compute.ListClustersResponse{ + Clusters: []compute.ClusterDetails{}, }, }, { @@ -574,17 +549,10 @@ func TestResourceClusterCreateNoWait_WithLibraries(t *testing.T) { }, }, { - Method: "POST", - Resource: "/api/2.1/clusters/events", - ExpectedRequest: compute.GetEvents{ - ClusterId: "abc", - Limit: 1, - Order: compute.GetEventsOrderDesc, - EventTypes: []compute.EventType{compute.EventTypePinned, compute.EventTypeUnpinned}, - }, - Response: compute.GetEventsResponse{ - Events: []compute.ClusterEvent{}, - TotalCount: 0, + Method: "GET", + Resource: "/api/2.1/clusters/list?filter_by.is_pinned=true&page_size=100", + Response: compute.ListClustersResponse{ + Clusters: []compute.ClusterDetails{}, }, }, { @@ -668,17 +636,10 @@ func TestResourceClusterCreateNoWait(t *testing.T) { }, }, { - Method: "POST", - Resource: "/api/2.1/clusters/events", - ExpectedRequest: compute.GetEvents{ - ClusterId: "abc", - Limit: 1, - Order: compute.GetEventsOrderDesc, - EventTypes: []compute.EventType{compute.EventTypePinned, compute.EventTypeUnpinned}, - }, - Response: compute.GetEventsResponse{ - Events: []compute.ClusterEvent{}, - TotalCount: 0, + Method: "GET", + Resource: "/api/2.1/clusters/list?filter_by.is_pinned=true&page_size=100", + Response: compute.ListClustersResponse{ + Clusters: []compute.ClusterDetails{}, }, }, }, @@ -745,17 +706,10 @@ func TestResourceClusterRead(t *testing.T) { }, }, { - Method: "POST", - Resource: "/api/2.1/clusters/events", - ExpectedRequest: compute.GetEvents{ - ClusterId: "abc", - Limit: 1, - Order: compute.GetEventsOrderDesc, - EventTypes: []compute.EventType{compute.EventTypePinned, compute.EventTypeUnpinned}, - }, - Response: compute.GetEventsResponse{ - Events: []compute.ClusterEvent{}, - TotalCount: 0, + Method: "GET", + Resource: "/api/2.1/clusters/list?filter_by.is_pinned=true&page_size=100", + Response: compute.ListClustersResponse{ + Clusters: []compute.ClusterDetails{}, }, }, }, @@ -843,17 +797,10 @@ func TestResourceClusterUpdate_ResizeForAutoscalingToNumWorkersCluster(t *testin }, }, { - Method: "POST", - Resource: "/api/2.1/clusters/events", - ExpectedRequest: compute.GetEvents{ - ClusterId: "abc", - Limit: 1, - Order: compute.GetEventsOrderDesc, - EventTypes: []compute.EventType{compute.EventTypePinned, compute.EventTypeUnpinned}, - }, - Response: compute.GetEventsResponse{ - Events: []compute.ClusterEvent{}, - TotalCount: 0, + Method: "GET", + Resource: "/api/2.1/clusters/list?filter_by.is_pinned=true&page_size=100", + Response: compute.ListClustersResponse{ + Clusters: []compute.ClusterDetails{}, }, }, { @@ -907,17 +854,10 @@ func TestResourceClusterUpdate_ResizeForNumWorkersToAutoscalingCluster(t *testin }, }, { - Method: "POST", - Resource: "/api/2.1/clusters/events", - ExpectedRequest: compute.GetEvents{ - ClusterId: "abc", - Limit: 1, - Order: compute.GetEventsOrderDesc, - EventTypes: []compute.EventType{compute.EventTypePinned, compute.EventTypeUnpinned}, - }, - Response: compute.GetEventsResponse{ - Events: []compute.ClusterEvent{}, - TotalCount: 0, + Method: "GET", + Resource: "/api/2.1/clusters/list?filter_by.is_pinned=true&page_size=100", + Response: compute.ListClustersResponse{ + Clusters: []compute.ClusterDetails{}, }, }, { @@ -974,17 +914,10 @@ func TestResourceClusterUpdate_EditNumWorkersWhenClusterTerminated(t *testing.T) }, }, { - Method: "POST", - Resource: "/api/2.1/clusters/events", - ExpectedRequest: compute.GetEvents{ - ClusterId: "abc", - Limit: 1, - Order: compute.GetEventsOrderDesc, - EventTypes: []compute.EventType{compute.EventTypePinned, compute.EventTypeUnpinned}, - }, - Response: compute.GetEventsResponse{ - Events: []compute.ClusterEvent{}, - TotalCount: 0, + Method: "GET", + Resource: "/api/2.1/clusters/list?filter_by.is_pinned=true&page_size=100", + Response: compute.ListClustersResponse{ + Clusters: []compute.ClusterDetails{}, }, }, { @@ -1050,17 +983,10 @@ func TestResourceClusterUpdate_ResizeAutoscale(t *testing.T) { }, }, { - Method: "POST", - Resource: "/api/2.1/clusters/events", - ExpectedRequest: compute.GetEvents{ - ClusterId: "abc", - Limit: 1, - Order: compute.GetEventsOrderDesc, - EventTypes: []compute.EventType{compute.EventTypePinned, compute.EventTypeUnpinned}, - }, - Response: compute.GetEventsResponse{ - Events: []compute.ClusterEvent{}, - TotalCount: 0, + Method: "GET", + Resource: "/api/2.1/clusters/list?filter_by.is_pinned=true&page_size=100", + Response: compute.ListClustersResponse{ + Clusters: []compute.ClusterDetails{}, }, }, }, @@ -1109,17 +1035,10 @@ func TestResourceClusterUpdate_ResizeNumWorkers(t *testing.T) { }, }, { - Method: "POST", - Resource: "/api/2.1/clusters/events", - ExpectedRequest: compute.GetEvents{ - ClusterId: "abc", - Limit: 1, - Order: compute.GetEventsOrderDesc, - EventTypes: []compute.EventType{compute.EventTypePinned, compute.EventTypeUnpinned}, - }, - Response: compute.GetEventsResponse{ - Events: []compute.ClusterEvent{}, - TotalCount: 0, + Method: "GET", + Resource: "/api/2.1/clusters/list?filter_by.is_pinned=true&page_size=100", + Response: compute.ListClustersResponse{ + Clusters: []compute.ClusterDetails{}, }, }, { @@ -1169,17 +1088,10 @@ func TestResourceClusterUpdate(t *testing.T) { }, }, { - Method: "POST", - Resource: "/api/2.1/clusters/events", - ExpectedRequest: compute.GetEvents{ - ClusterId: "abc", - Limit: 1, - Order: compute.GetEventsOrderDesc, - EventTypes: []compute.EventType{compute.EventTypePinned, compute.EventTypeUnpinned}, - }, - Response: compute.GetEventsResponse{ - Events: []compute.ClusterEvent{}, - TotalCount: 0, + Method: "GET", + Resource: "/api/2.1/clusters/list?filter_by.is_pinned=true&page_size=100", + Response: compute.ListClustersResponse{ + Clusters: []compute.ClusterDetails{}, }, }, { @@ -1249,17 +1161,10 @@ func TestResourceClusterUpdate_WhileScaling(t *testing.T) { }, }, { - Method: "POST", - Resource: "/api/2.1/clusters/events", - ExpectedRequest: compute.GetEvents{ - ClusterId: "abc", - Limit: 1, - Order: compute.GetEventsOrderDesc, - EventTypes: []compute.EventType{compute.EventTypePinned, compute.EventTypeUnpinned}, - }, - Response: compute.GetEventsResponse{ - Events: []compute.ClusterEvent{}, - TotalCount: 0, + Method: "GET", + Resource: "/api/2.1/clusters/list?filter_by.is_pinned=true&page_size=100", + Response: compute.ListClustersResponse{ + Clusters: []compute.ClusterDetails{}, }, }, { @@ -1345,17 +1250,10 @@ func TestResourceClusterUpdateWithPinned(t *testing.T) { }, }, { - Method: "POST", - Resource: "/api/2.1/clusters/events", - ExpectedRequest: compute.GetEvents{ - ClusterId: "abc", - Limit: 1, - Order: compute.GetEventsOrderDesc, - EventTypes: []compute.EventType{compute.EventTypePinned, compute.EventTypeUnpinned}, - }, - Response: compute.GetEventsResponse{ - Events: []compute.ClusterEvent{}, - TotalCount: 0, + Method: "GET", + Resource: "/api/2.1/clusters/list?filter_by.is_pinned=true&page_size=100", + Response: compute.ListClustersResponse{ + Clusters: []compute.ClusterDetails{}, }, }, { @@ -1493,17 +1391,10 @@ func TestResourceClusterUpdate_LibrariesChangeOnTerminatedCluster(t *testing.T) }, }, { - Method: "POST", - Resource: "/api/2.1/clusters/events", - ExpectedRequest: compute.GetEvents{ - ClusterId: "abc", - Limit: 1, - Order: compute.GetEventsOrderDesc, - EventTypes: []compute.EventType{compute.EventTypePinned, compute.EventTypeUnpinned}, - }, - Response: compute.GetEventsResponse{ - Events: []compute.ClusterEvent{}, - TotalCount: 0, + Method: "GET", + Resource: "/api/2.1/clusters/list?filter_by.is_pinned=true&page_size=100", + Response: compute.ListClustersResponse{ + Clusters: []compute.ClusterDetails{}, }, }, { // start cluster before libs install @@ -1633,17 +1524,10 @@ func TestResourceClusterUpdate_AutoAz(t *testing.T) { }, }, { - Method: "POST", - Resource: "/api/2.1/clusters/events", - ExpectedRequest: compute.GetEvents{ - ClusterId: "abc", - Limit: 1, - Order: compute.GetEventsOrderDesc, - EventTypes: []compute.EventType{compute.EventTypePinned, compute.EventTypeUnpinned}, - }, - Response: compute.GetEventsResponse{ - Events: []compute.ClusterEvent{}, - TotalCount: 0, + Method: "GET", + Resource: "/api/2.1/clusters/list?filter_by.is_pinned=true&page_size=100", + Response: compute.ListClustersResponse{ + Clusters: []compute.ClusterDetails{}, }, }, { @@ -1786,17 +1670,10 @@ func TestResourceClusterCreate_SingleNode(t *testing.T) { }, }, { - Method: "POST", - Resource: "/api/2.1/clusters/events", - ExpectedRequest: compute.GetEvents{ - ClusterId: "abc", - Limit: 1, - Order: compute.GetEventsOrderDesc, - EventTypes: []compute.EventType{compute.EventTypePinned, compute.EventTypeUnpinned}, - }, - Response: compute.GetEventsResponse{ - Events: []compute.ClusterEvent{}, - TotalCount: 0, + Method: "GET", + Resource: "/api/2.1/clusters/list?filter_by.is_pinned=true&page_size=100", + Response: compute.ListClustersResponse{ + Clusters: []compute.ClusterDetails{}, }, }, { @@ -1970,8 +1847,11 @@ func TestReadOnStoppedClusterWithLibrariesDoesNotFail(t *testing.T) { }, }, { - Method: "POST", - Resource: "/api/2.1/clusters/events", + Method: "GET", + Resource: "/api/2.1/clusters/list?filter_by.is_pinned=true&page_size=100", + Response: compute.ListClustersResponse{ + Clusters: []compute.ClusterDetails{}, + }, }, { Method: "GET", @@ -2008,8 +1888,11 @@ func TestRefreshOnRunningClusterWithFailedLibraryUninstallsIt(t *testing.T) { }, }, { - Method: "POST", - Resource: "/api/2.1/clusters/events", + Method: "GET", + Resource: "/api/2.1/clusters/list?filter_by.is_pinned=true&page_size=100", + Response: compute.ListClustersResponse{ + Clusters: []compute.ClusterDetails{}, + }, }, { Method: "GET", @@ -2072,17 +1955,10 @@ func TestResourceClusterUpdate_LocalSsdCount(t *testing.T) { }, }, { - Method: "POST", - Resource: "/api/2.1/clusters/events", - ExpectedRequest: compute.GetEvents{ - ClusterId: "abc", - Limit: 1, - Order: compute.GetEventsOrderDesc, - EventTypes: []compute.EventType{compute.EventTypePinned, compute.EventTypeUnpinned}, - }, - Response: compute.GetEventsResponse{ - Events: []compute.ClusterEvent{}, - TotalCount: 0, + Method: "GET", + Resource: "/api/2.1/clusters/list?filter_by.is_pinned=true&page_size=100", + Response: compute.ListClustersResponse{ + Clusters: []compute.ClusterDetails{}, }, }, { diff --git a/exporter/exporter_test.go b/exporter/exporter_test.go index ad485b955..120538efd 100644 --- a/exporter/exporter_test.go +++ b/exporter/exporter_test.go @@ -836,9 +836,12 @@ func TestImportingClusters(t *testing.T) { ReuseRequest: true, }, { - Method: "POST", - Resource: "/api/2.1/clusters/events", - Response: compute.GetEvents{}, + Method: "GET", + Resource: "/api/2.1/clusters/list?filter_by.is_pinned=true&page_size=100", + Response: compute.ListClustersResponse{ + Clusters: []compute.ClusterDetails{}, + }, + ReuseRequest: true, }, { Method: "GET", @@ -868,30 +871,6 @@ func TestImportingClusters(t *testing.T) { Resource: "/api/2.1/clusters/get?cluster_id=test2", Response: getJSONObject("test-data/get-cluster-test2-response.json"), }, - { - Method: "POST", - Resource: "/api/2.1/clusters/events", - ExpectedRequest: compute.GetEvents{ - ClusterId: "test2", - Order: compute.GetEventsOrderDesc, - EventTypes: []compute.EventType{compute.EventTypePinned, compute.EventTypeUnpinned}, - Limit: 1, - }, - Response: compute.EventDetails{}, - ReuseRequest: true, - }, - { - Method: "POST", - Resource: "/api/2.1/clusters/events", - ExpectedRequest: compute.GetEvents{ - ClusterId: "test1", - Order: compute.GetEventsOrderDesc, - EventTypes: []compute.EventType{compute.EventTypePinned, compute.EventTypeUnpinned}, - Limit: 1, - }, - Response: compute.EventDetails{}, - ReuseRequest: true, - }, { Method: "GET", Resource: "/api/2.0/libraries/cluster-status?cluster_id=test2", @@ -917,17 +896,6 @@ func TestImportingClusters(t *testing.T) { Resource: "/api/2.1/clusters/get?cluster_id=awscluster", Response: getJSONObject("test-data/get-cluster-awscluster-response.json"), }, - { - Method: "POST", - Resource: "/api/2.1/clusters/events", - ExpectedRequest: compute.GetEvents{ - ClusterId: "awscluster", - Order: compute.GetEventsOrderDesc, - EventTypes: []compute.EventType{compute.EventTypePinned, compute.EventTypeUnpinned}, - Limit: 1, - }, - Response: compute.EventDetails{}, - }, { Method: "GET", Resource: "/api/2.0/libraries/cluster-status?cluster_id=awscluster",