Skip to content

Commit 7a67954

Browse files
authored
cmd: Handle case when deployment template does not include resource (#205)
Fixes a bug where the application would panic if it could not find the desired resource kind in the deployment template.
1 parent 0df4211 commit 7a67954

File tree

4 files changed

+57
-1
lines changed

4 files changed

+57
-1
lines changed

pkg/deployment/depresource/apm.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
package depresource
1919

2020
import (
21+
"fmt"
22+
2123
"github.com/elastic/cloud-sdk-go/pkg/api"
2224
"github.com/elastic/cloud-sdk-go/pkg/client/platform_configuration_templates"
2325
"github.com/elastic/cloud-sdk-go/pkg/models"
@@ -56,6 +58,11 @@ func NewApm(params NewStateless) (*models.ApmPayload, error) {
5658
return nil, api.UnwrapError(err)
5759
}
5860

61+
if res.Payload.ClusterTemplate.Apm == nil {
62+
return nil, fmt.Errorf("deployment: the %s template is not configured for APM. Please use another template if you wish to start APM instances",
63+
params.TemplateID)
64+
}
65+
5966
var clusterTopology = res.Payload.ClusterTemplate.Apm.Plan.ClusterTopology
6067
var topology = models.ApmTopologyElement{Size: new(models.TopologySize)}
6168
if len(clusterTopology) > 0 {

pkg/deployment/depresource/apm_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ var apmTemplateResponse = models.DeploymentTemplateInfo{
5454
},
5555
}
5656

57+
var crossClusterTemplateResponse = models.DeploymentTemplateInfo{
58+
ID: "cross-cluster-search",
59+
ClusterTemplate: &models.DeploymentTemplateDefinitionRequest{
60+
Plan: &models.ElasticsearchClusterPlan{
61+
ClusterTopology: defaultESTopologies,
62+
},
63+
},
64+
}
65+
5766
func TestNewApm(t *testing.T) {
5867
var internalError = models.BasicFailedReply{
5968
Errors: []*models.BasicFailedReplyElement{
@@ -139,6 +148,18 @@ func TestNewApm(t *testing.T) {
139148
}},
140149
err: errors.New(string(internalErrorBytes)),
141150
},
151+
{
152+
name: "obtains the deployment template but it's an invalid template for appsearch",
153+
args: args{params: NewStateless{
154+
DeploymentID: util.ValidClusterID,
155+
API: api.NewMock(
156+
mock.New200Response(mock.NewStructBody(getResponse)),
157+
mock.New200Response(mock.NewStructBody(crossClusterTemplateResponse)),
158+
),
159+
Region: "ece-region",
160+
}},
161+
err: errors.New("deployment: the an ID template is not configured for APM. Please use another template if you wish to start APM instances"),
162+
},
142163
{
143164
name: "succeeds with no argument override",
144165
args: args{params: NewStateless{

pkg/deployment/depresource/appsearch.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
package depresource
1919

2020
import (
21+
"fmt"
22+
2123
"github.com/elastic/cloud-sdk-go/pkg/api"
2224
"github.com/elastic/cloud-sdk-go/pkg/client/platform_configuration_templates"
2325
"github.com/elastic/cloud-sdk-go/pkg/models"
@@ -56,6 +58,11 @@ func NewAppSearch(params NewStateless) (*models.AppSearchPayload, error) {
5658
return nil, api.UnwrapError(err)
5759
}
5860

61+
if res.Payload.ClusterTemplate.Appsearch == nil {
62+
return nil, fmt.Errorf("deployment: the %s template is not configured for AppSearch. Please use another template if you wish to start AppSearch instances",
63+
params.TemplateID)
64+
}
65+
5966
var clusterTopology = res.Payload.ClusterTemplate.Appsearch.Plan.ClusterTopology
6067
var topology = models.AppSearchTopologyElement{Size: new(models.TopologySize)}
6168
if len(clusterTopology) > 0 {

pkg/deployment/depresource/appsearch_test.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import (
3333
)
3434

3535
var appsearchTemplateResponse = models.DeploymentTemplateInfo{
36-
ID: "default",
36+
ID: "default.appsearch",
3737
ClusterTemplate: &models.DeploymentTemplateDefinitionRequest{
3838
Appsearch: &models.CreateAppSearchRequest{
3939
Plan: &models.AppSearchPlan{
@@ -54,6 +54,15 @@ var appsearchTemplateResponse = models.DeploymentTemplateInfo{
5454
},
5555
}
5656

57+
var defaultTemplateResponse = models.DeploymentTemplateInfo{
58+
ID: "default",
59+
ClusterTemplate: &models.DeploymentTemplateDefinitionRequest{
60+
Plan: &models.ElasticsearchClusterPlan{
61+
ClusterTopology: defaultESTopologies,
62+
},
63+
},
64+
}
65+
5766
func TestNewAppSearch(t *testing.T) {
5867
var internalError = models.BasicFailedReply{
5968
Errors: []*models.BasicFailedReplyElement{
@@ -139,6 +148,18 @@ func TestNewAppSearch(t *testing.T) {
139148
}},
140149
err: errors.New(string(internalErrorBytes)),
141150
},
151+
{
152+
name: "obtains the deployment template but it's an invalid template for appsearch",
153+
args: args{params: NewStateless{
154+
DeploymentID: util.ValidClusterID,
155+
API: api.NewMock(
156+
mock.New200Response(mock.NewStructBody(getResponse)),
157+
mock.New200Response(mock.NewStructBody(defaultTemplateResponse)),
158+
),
159+
Region: "ece-region",
160+
}},
161+
err: errors.New("deployment: the an ID template is not configured for AppSearch. Please use another template if you wish to start AppSearch instances"),
162+
},
142163
{
143164
name: "succeeds with no argument override",
144165
args: args{params: NewStateless{

0 commit comments

Comments
 (0)