-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
251 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
// Copyright Mia srl | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package resources | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/mia-platform/miactl/internal/client" | ||
"github.com/mia-platform/miactl/internal/clioptions" | ||
"github.com/mia-platform/miactl/internal/resources" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
const ( | ||
createJobTemplate = "/api/projects/%s/environments/%s/jobs/" | ||
) | ||
|
||
func CreateCommand(options *clioptions.CLIOptions) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "create", | ||
Short: "Create Mia-Platform Console runtime resources", | ||
Long: "Create Mia-Platform Console runtime resources.", | ||
} | ||
|
||
// add cmd flags | ||
|
||
// add sub commands | ||
cmd.AddCommand( | ||
jobCommand(options), | ||
) | ||
|
||
return cmd | ||
} | ||
|
||
func jobCommand(options *clioptions.CLIOptions) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "job ENVIRONMENT", | ||
Short: "Create a job from a cronjob in the selected environment and project", | ||
Long: "Create a job from a cronjob in the selected environment and project", | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
restConfig, err := options.ToRESTConfig() | ||
cobra.CheckErr(err) | ||
client, err := client.APIClientForConfig(restConfig) | ||
cobra.CheckErr(err) | ||
return createJob(client, restConfig.ProjectID, args[0], options.FromCronJob) | ||
}, | ||
} | ||
|
||
// add cmd flags | ||
options.AddCreateJobFlags(cmd.Flags()) | ||
if err := cmd.MarkFlagRequired("from"); err != nil { | ||
// programming error, panic and broke everything | ||
panic(err) | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func createJob(client *client.APIClient, projectID, environment, cronjobName string) error { | ||
if projectID == "" { | ||
return fmt.Errorf("missing project id, please set one with the flag or context") | ||
} | ||
|
||
requestBody := &resources.CreateJobRequest{ | ||
From: "cronjob", | ||
ResourceName: cronjobName, | ||
} | ||
|
||
bodyBytes, err := json.Marshal(requestBody) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
response, err := client. | ||
Post(). | ||
APIPath(fmt.Sprintf(createJobTemplate, projectID, environment)). | ||
Body(bodyBytes). | ||
Do(context.Background()) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := response.Error(); err != nil { | ||
return err | ||
} | ||
|
||
var createResponse resources.CreateJob | ||
if err := response.ParseResponse(&createResponse); err != nil { | ||
return err | ||
} | ||
|
||
fmt.Printf("Job %s create successfully!\n", createResponse.JobName) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// Copyright Mia srl | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package resources | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/mia-platform/miactl/internal/client" | ||
"github.com/mia-platform/miactl/internal/resources" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCreateJob(t *testing.T) { | ||
testCases := map[string]struct { | ||
testServer *httptest.Server | ||
projectID string | ||
err bool | ||
}{ | ||
"create job end with success": { | ||
testServer: createJobTestServer(t), | ||
projectID: "success", | ||
}, | ||
"create job end with error": { | ||
testServer: createJobTestServer(t), | ||
projectID: "fail", | ||
err: true, | ||
}, | ||
} | ||
|
||
for testName, testCase := range testCases { | ||
t.Run(testName, func(t *testing.T) { | ||
server := testCase.testServer | ||
defer server.Close() | ||
|
||
client, err := client.APIClientForConfig(&client.Config{ | ||
Host: server.URL, | ||
}) | ||
require.NoError(t, err) | ||
|
||
err = createJob(client, testCase.projectID, "env-id", "cronjob-name") | ||
if testCase.err { | ||
require.Error(t, err) | ||
} else { | ||
require.NoError(t, err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func createJobTestServer(t *testing.T) *httptest.Server { | ||
t.Helper() | ||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
switch { | ||
case r.Method == http.MethodPost && r.URL.Path == fmt.Sprintf(createJobTemplate, "success", "env-id"): | ||
response := resources.CreateJob{ | ||
JobName: "new-job-name", | ||
} | ||
data, err := resources.EncodeResourceToJSON(response) | ||
assert.NoError(t, err) | ||
w.WriteHeader(http.StatusOK) | ||
w.Write(data) | ||
case r.Method == http.MethodPost && r.URL.Path == fmt.Sprintf(createJobTemplate, "fail", "env-id"): | ||
response := resources.APIError{ | ||
StatusCode: http.StatusInternalServerError, | ||
Message: "Error creating job", | ||
} | ||
data, err := resources.EncodeResourceToJSON(response) | ||
assert.NoError(t, err) | ||
w.WriteHeader(http.StatusInternalServerError) | ||
w.Write(data) | ||
default: | ||
w.WriteHeader(http.StatusNotFound) | ||
assert.Failf(t, "unexpected http call", "received call with method: %s uri %s", r.Method, r.RequestURI) | ||
} | ||
})) | ||
return server | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters