Skip to content

Commit c95a04e

Browse files
author
Oleg Sucharevich
committed
Support runtime environment creation
1 parent ef30bf9 commit c95a04e

File tree

4 files changed

+113
-1
lines changed

4 files changed

+113
-1
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.0.3
1+
0.1.0

cmd/create_runtime_environment.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright © 2018 Codefresh.Inc
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package cmd
16+
17+
import (
18+
"fmt"
19+
20+
"github.com/codefresh-io/go-sdk/pkg/codefresh"
21+
22+
"github.com/codefresh-io/go-sdk/pkg/utils"
23+
"github.com/spf13/cobra"
24+
"github.com/spf13/viper"
25+
)
26+
27+
// createRuntimeEnvironmentCmd represents the createRuntimeEnvironment command
28+
var createRuntimeEnvironmentCmd = &cobra.Command{
29+
Use: "runtime-environment",
30+
Aliases: []string{"re"},
31+
Short: "Create runtime environment",
32+
33+
Run: func(cmd *cobra.Command, args []string) {
34+
client := viper.Get("codefresh")
35+
codefreshClient := utils.CastToCodefreshOrDie(client)
36+
cluster := cmd.Flag("cluster").Value.String()
37+
namespace := cmd.Flag("namespace").Value.String()
38+
opt := &codefresh.CreateRuntimeOptions{
39+
Cluster: cluster,
40+
Namespace: namespace,
41+
HasAgent: true,
42+
}
43+
re, err := codefreshClient.CreateRuntimeEnvironment(opt)
44+
if err == nil {
45+
fmt.Printf("Runtime-Environment %s created\n", re.Name)
46+
}
47+
},
48+
}
49+
50+
func init() {
51+
createCmd.AddCommand(createRuntimeEnvironmentCmd)
52+
createRuntimeEnvironmentCmd.Flags().String("cluster", "", "Set name of the cluster (required)")
53+
createRuntimeEnvironmentCmd.MarkFlagRequired("cluster")
54+
createRuntimeEnvironmentCmd.Flags().String("namespace", "", "Set name of the namespace (required)")
55+
createRuntimeEnvironmentCmd.MarkFlagRequired("namespace")
56+
createRuntimeEnvironmentCmd.Flags().Bool("has-agent", false, "Set if the runtime environment is managed by Codefresh agent")
57+
}

pkg/codefresh/codefresh.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ type (
1313
requestAPI(*requestOptions) (*gentleman.Response, error)
1414
ITokenAPI
1515
IPipelineAPI
16+
IRuntimeEnvironmentAPI
1617
}
1718
)
1819

pkg/codefresh/runtime_enrionment.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package codefresh
2+
3+
import "fmt"
4+
5+
type (
6+
// IRuntimeEnvironmentAPI declers Codefresh runtime environment API
7+
IRuntimeEnvironmentAPI interface {
8+
CreateRuntimeEnvironment(*CreateRuntimeOptions) (*RuntimeEnvironment, error)
9+
}
10+
11+
RuntimeEnvironment struct {
12+
Name string
13+
}
14+
15+
CreateRuntimeOptions struct {
16+
Cluster string
17+
Namespace string
18+
HasAgent bool
19+
}
20+
21+
createRuntimeEnvironmentResponse struct {
22+
Name string
23+
}
24+
)
25+
26+
// CreateRuntimeEnvironment - returns pipelines from API
27+
func (c *codefresh) CreateRuntimeEnvironment(opt *CreateRuntimeOptions) (*RuntimeEnvironment, error) {
28+
// r := &createRuntimeEnvironmentResponse{}
29+
re := &RuntimeEnvironment{
30+
Name: fmt.Sprintf("%s/%s", opt.Cluster, opt.Namespace),
31+
}
32+
body := map[string]interface{}{
33+
"clusterName": opt.Cluster,
34+
"namespace": opt.Namespace,
35+
}
36+
if opt.HasAgent {
37+
body["agent"] = true
38+
}
39+
resp, err := c.requestAPI(&requestOptions{
40+
path: "/api/custom_clusters/register",
41+
method: "POST",
42+
body: body,
43+
})
44+
45+
if err != nil {
46+
return nil, err
47+
}
48+
49+
if resp.StatusCode < 400 {
50+
return re, nil
51+
} else {
52+
return nil, fmt.Errorf("Error during runtime environment creation")
53+
}
54+
}

0 commit comments

Comments
 (0)