Skip to content

Commit

Permalink
[CP-5351] Remove Apm from create payload when version >= 8.0.0 (#633)
Browse files Browse the repository at this point in the history
* Remove Apm from create payload when version >= 8.0.0.
* Add semver as an explicit requirement.
  • Loading branch information
dimuon committed Dec 14, 2023
1 parent cf7edbe commit 03cc42c
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 34 deletions.
64 changes: 32 additions & 32 deletions NOTICE
Expand Up @@ -36,6 +36,38 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

--------------------------------------------------------------------------------

Dependency : github.com/blang/semver/v4
Version: v4.0.0
Licence type (autodetected): MIT

Contents of probable licence file $GOMODCACHE/github.com/blang/semver/v4@v4.0.0/LICENSE:

The MIT License

Copyright (c) 2014 Benedikt Lang <github at benediktlang.de>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.



--------------------------------------------------------------------------------

Dependency : github.com/elastic/cloud-sdk-go
Expand Down Expand Up @@ -1033,38 +1065,6 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Indirect dependencies
================================================================================

Dependency : github.com/blang/semver/v4
Version: v4.0.0
Licence type (autodetected): MIT

Contents of probable licence file $GOMODCACHE/github.com/blang/semver/v4@v4.0.0/LICENSE:

The MIT License

Copyright (c) 2014 Benedikt Lang <github at benediktlang.de>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.



--------------------------------------------------------------------------------

Dependency : github.com/cpuguy83/go-md2man/v2
Version: v2.0.2
Licence type (autodetected): MIT
Expand Down
14 changes: 13 additions & 1 deletion cmd/deployment/create.go
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"strings"

"github.com/blang/semver/v4"
"github.com/elastic/cloud-sdk-go/pkg/api/deploymentapi"
"github.com/elastic/cloud-sdk-go/pkg/api/deploymentapi/deptemplateapi"
"github.com/elastic/cloud-sdk-go/pkg/models"
Expand Down Expand Up @@ -166,5 +167,16 @@ func newCreatePayload(cmd *cobra.Command, version, region string) (*models.Deplo
es[0].Plan.DeploymentTemplate.ID = &dt
}

return tpl.DeploymentTemplate, nil
return removeApmForVersions8(version, tpl.DeploymentTemplate)
}

func removeApmForVersions8(version string, tpl *models.DeploymentCreateRequest) (*models.DeploymentCreateRequest, error) {
vers, err := semver.Parse(version)
if err != nil {
return nil, fmt.Errorf("failed to parse version: %v", err)
}
if vers.Major >= 8 {
tpl.Resources.Apm = nil
}
return tpl, nil
}
148 changes: 148 additions & 0 deletions cmd/deployment/create_test.go
Expand Up @@ -26,6 +26,8 @@ import (
"net/url"
"testing"

"github.com/stretchr/testify/require"

"github.com/elastic/cloud-sdk-go/pkg/api"
"github.com/elastic/cloud-sdk-go/pkg/api/mock"
"github.com/elastic/cloud-sdk-go/pkg/models"
Expand Down Expand Up @@ -452,6 +454,57 @@ Deployment [%s] - [Apm][%s]: running step "waiting-for-some-step" (Plan duration
"\n" + "Request ID: some_request_id" + "\n",
},
},
{
name: "removes Apm from payload when creating deployment with default values if version is 8.x",
args: testutils.Args{
Cmd: createCmd,
Args: []string{
"create",
"--version=8.11.3",
"--deployment-template=aws-io-optimized-v2",
"--request-id=some_request_id",
"--name=with_default",
},
Cfg: testutils.MockCfg{Responses: []mock.Response{
{
Response: http.Response{
StatusCode: 200,
Body: mock.NewByteBody(awsIoOptimisedTemplate),
},
Assert: &mock.RequestAssertion{
Method: "GET",
Header: api.DefaultReadMockHeaders,
Path: "/api/v1/deployments/templates/aws-io-optimized-v2",
Host: api.DefaultMockHost,
Query: url.Values{
"region": {"ece-region"},
"show_instance_configurations": {"true"},
"stack_version": {"8.11.3"},
},
},
},
{
Response: http.Response{
StatusCode: 201,
Body: mock.NewStructBody(defaultCreateResponse),
},
Assert: &mock.RequestAssertion{
Method: "POST",
Header: api.DefaultWriteMockHeaders,
Body: mock.NewStringBody(`{"name":"with_default","resources":{"apm":null,"appsearch":null,"elasticsearch":[{"plan":{"autoscaling_enabled":false,"cluster_topology":[{"id":"coordinating","instance_configuration_id":"aws.coordinating.m5d","node_roles":["ingest","remote_cluster_client"],"size":{"resource":"memory","value":0},"topology_element_control":{"min":{"resource":"memory","value":0}},"zone_count":2},{"autoscaling_max":{"resource":"memory","value":118784},"elasticsearch":{"node_attributes":{"data":"hot"}},"id":"hot_content","instance_configuration_id":"aws.data.highio.i3","node_roles":["master","ingest","remote_cluster_client","data_hot","transform","data_content"],"size":{"resource":"memory","value":8192},"topology_element_control":{"min":{"resource":"memory","value":1024}},"zone_count":2},{"autoscaling_max":{"resource":"memory","value":118784},"elasticsearch":{"node_attributes":{"data":"warm"}},"id":"warm","instance_configuration_id":"aws.data.highstorage.d3","node_roles":["data_warm","remote_cluster_client"],"size":{"resource":"memory","value":0},"topology_element_control":{"min":{"resource":"memory","value":0}},"zone_count":2},{"autoscaling_max":{"resource":"memory","value":59392},"elasticsearch":{"node_attributes":{"data":"cold"}},"id":"cold","instance_configuration_id":"aws.data.highstorage.d3","node_roles":["data_cold","remote_cluster_client"],"size":{"resource":"memory","value":0},"topology_element_control":{"min":{"resource":"memory","value":0}},"zone_count":1},{"id":"master","instance_configuration_id":"aws.master.r5d","node_roles":["master"],"size":{"resource":"memory","value":0},"topology_element_control":{"min":{"resource":"memory","value":0}},"zone_count":3},{"autoscaling_max":{"resource":"memory","value":61440},"autoscaling_min":{"resource":"memory","value":0},"id":"ml","instance_configuration_id":"aws.ml.m5d","node_roles":["ml","remote_cluster_client"],"size":{"resource":"memory","value":0},"topology_element_control":{"min":{"resource":"memory","value":0}},"zone_count":1}],"deployment_template":{"id":"aws-io-optimized-v2"},"elasticsearch":{"version":"8.11.3"}},"ref_id":"main-elasticsearch","region":"us-east-1","settings":{"dedicated_masters_threshold":6}}],"enterprise_search":[{"elasticsearch_cluster_ref_id":"main-elasticsearch","plan":{"cluster_topology":[{"instance_configuration_id":"aws.enterprisesearch.m5d","node_type":{"appserver":true,"connector":true,"worker":true},"size":{"resource":"memory","value":0},"zone_count":2}],"enterprise_search":{"version":"8.11.3"}},"ref_id":"main-enterprise_search","region":"us-east-1"}],"integrations_server":null,"kibana":[{"elasticsearch_cluster_ref_id":"main-elasticsearch","plan":{"cluster_topology":[{"instance_configuration_id":"aws.kibana.r5d","size":{"resource":"memory","value":1024},"zone_count":1}],"kibana":{"version":"8.11.3"}},"ref_id":"main-kibana","region":"us-east-1"}]}}` + "\n"),
Path: "/api/v1/deployments",
Host: api.DefaultMockHost,
Query: url.Values{
"request_id": {"some_request_id"},
},
},
},
}},
},
want: testutils.Assertion{
Stdout: string(defaultCreateResponseBytes),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -461,3 +514,98 @@ Deployment [%s] - [Apm][%s]: running step "waiting-for-some-step" (Plan duration
})
}
}

func Test_newCreatePayload(t *testing.T) {
type args struct {
version string
tpl *models.DeploymentCreateRequest
}
tests := []struct {
name string
args args
expected *models.DeploymentCreateRequest
err error
}{
{
name: "fails if it cannot parse version",
args: args{
version: "bad version",
},
err: fmt.Errorf("failed to parse version: No Major.Minor.Patch elements found"),
},
{
name: "returns payload from template as-is including Apm if version < 8.0.0",
args: args{
version: "7.17.16",
tpl: &models.DeploymentCreateRequest{
Resources: &models.DeploymentCreateResources{
Apm: []*models.ApmPayload{
{
DisplayName: "test-apm",
},
},
Elasticsearch: []*models.ElasticsearchPayload{
{
DisplayName: "test-elasticsearch",
},
},
},
},
},
expected: &models.DeploymentCreateRequest{
Resources: &models.DeploymentCreateResources{
Apm: []*models.ApmPayload{
{
DisplayName: "test-apm",
},
},
Elasticsearch: []*models.ElasticsearchPayload{
{
DisplayName: "test-elasticsearch",
},
},
},
},
},
{
name: "returns payload from template but without Apm if version >= 8.0.0",
args: args{
version: "8.0.0",
tpl: &models.DeploymentCreateRequest{
Resources: &models.DeploymentCreateResources{
Apm: []*models.ApmPayload{
{
DisplayName: "test-apm",
},
},
Elasticsearch: []*models.ElasticsearchPayload{
{
DisplayName: "test-elasticsearch",
},
},
},
},
},
expected: &models.DeploymentCreateRequest{
Resources: &models.DeploymentCreateResources{
Elasticsearch: []*models.ElasticsearchPayload{
{
DisplayName: "test-elasticsearch",
},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
res, err := removeApmForVersions8(tt.args.version, tt.args.tpl)
if tt.err != nil {
require.Equal(t, tt.err, err)
require.Nil(t, res)
} else {
require.Equal(t, *tt.expected, *res)
}
})
}
}
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -4,6 +4,7 @@ go 1.20

require (
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d
github.com/blang/semver/v4 v4.0.0
github.com/elastic/cloud-sdk-go v1.14.0
github.com/go-openapi/runtime v0.23.0
github.com/go-openapi/strfmt v0.21.2
Expand All @@ -16,7 +17,6 @@ require (
)

require (
github.com/blang/semver/v4 v4.0.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
Expand Down

0 comments on commit 03cc42c

Please sign in to comment.