Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Hernandez <jhernand@redhat.com>
  • Loading branch information
jhernand authored and vkareh committed May 11, 2020
1 parent 284d531 commit 357b274
Show file tree
Hide file tree
Showing 15 changed files with 709 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea/
/moactl
4 changes: 4 additions & 0 deletions CHANGES.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
= Changes

This document describes the relevant changes between releases of the `moactl`
command line tool.
48 changes: 48 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#
# Copyright (c) 2020 Red Hat, Inc.
#
# 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.
#

# Ensure go modules are enabled:
export GO111MODULE=on
export GOPROXY=https://proxy.golang.org

# Disable CGO so that we always generate static binaries:
export CGO_ENABLED=0

.PHONY: moactl
moactl:
go build .

.PHONY: test
test:
ginkgo -r cmd pkg

.PHONY: fmt
fmt:
gofmt -s -l -w cmd pkg

.PHONY: lint
lint:
golangci-lint run

.PHONY: clean
clean:
rm -rf \
moactl \
*-darwin-amd64 \
*-linux-amd64 \
*-windows-amd64 \
*.sha256 \
$(NULL)
4 changes: 4 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
= MOA Command Line Tool

This project contains the `moactl` command line tool that simplifies the use of
the _MOA_.
33 changes: 33 additions & 0 deletions cmd/cluster/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
Copyright (c) 2020 Red Hat, Inc.
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 cluster

import (
"github.com/spf13/cobra"

"gitlab.cee.redhat.com/service/moactl/cmd/cluster/create"
)

var Cmd = &cobra.Command{
Use: "cluster COMMAND",
Short: "Perform cluster operations",
Long: "Perform cluster operations.",
}

func init() {
Cmd.AddCommand(create.Cmd)
}
129 changes: 129 additions & 0 deletions cmd/cluster/create/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
Copyright (c) 2020 Red Hat, Inc.
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 create

import (
"fmt"
"os"

"github.com/openshift-online/ocm-sdk-go"
cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
"github.com/spf13/cobra"

"gitlab.cee.redhat.com/service/moactl/pkg/debug"
rprtr "gitlab.cee.redhat.com/service/moactl/pkg/reporter"
)

var args struct {
region string
}

var Cmd = &cobra.Command{
Use: "create [FLAGS] NAME",
Short: "Create cluster",
Long: "Create cluster.",
Run: run,
}

func init() {
fs := Cmd.Flags()
fs.StringVar(
&args.region,
"region",
"us-east-1",
"Region to create the cluster in.",
)
}

func run(cmd *cobra.Command, argv []string) {
// Create the reporter:
reporter, err := rprtr.New().
Build()
if err != nil {
fmt.Errorf("Can't create reporter: %v\n", err)
os.Exit(1)
}

// Check command line arguments:
if len(argv) != 1 {
reporter.Errorf(
"Expected exactly one command line parameter containing the name " +
"of the cluster",
)
os.Exit(1)
}
name := argv[0]

// Check the command line options:
if args.region == "" {
reporter.Errorf("Option '--region' is mandatory")
os.Exit(1)
}

// Check that there is an OCM token in the environment. This will not be needed once we are
// able to derive OCM credentials from AWS credentials.
ocmToken := os.Getenv("OCM_TOKEN")
if ocmToken == "" {
reporter.Errorf("Environment variable 'OCM_TOKEN' is not set")
os.Exit(1)
}

// Create the logger that will be used by the OCM connection:
ocmLogger, err := sdk.NewStdLoggerBuilder().
Debug(debug.Enabled()).
Build()
if err != nil {
reporter.Errorf("Can't create OCM logger: %v", err)
os.Exit(1)
}

// Create the client for the OCM API:
ocmConnection, err := sdk.NewConnectionBuilder().
Logger(ocmLogger).
Tokens(ocmToken).
URL("https://api-integration.6943.hive-integration.openshiftapps.com").
Build()
if err != nil {
reporter.Errorf("Can't create OCM connection: %v", err)
os.Exit(1)
}

// Create the permissions needed to create the cluster:
reporter.Infof("Creating permissions")

// Create the cluster:
reporter.Infof("Creating cluster '%s'", name)
ocmCluster, err := cmv1.NewCluster().
Name(name).
Region(
cmv1.NewCloudRegion().
ID(args.region),
).
Build()
if err != nil {
reporter.Errorf("Can't create description of OCM cluster: %v", err)
os.Exit(1)
}
_, err = ocmConnection.ClustersMgmt().V1().Clusters().Add().
Body(ocmCluster).
Send()
if err != nil {
reporter.Infof("Can't create OCM cluster: %v", err)
os.Exit(1)
}
reporter.Infof("Cluster '%s' is being created", name)
}
37 changes: 37 additions & 0 deletions cmd/version/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
Copyright (c) 2020 Red Hat, Inc.
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 version

import (
"fmt"
"os"

"github.com/spf13/cobra"

"gitlab.cee.redhat.com/service/moactl/pkg/info"
)

var Cmd = &cobra.Command{
Use: "version",
Short: "Prints the version of the tool",
Long: "Prints the version number of the tool.",
Run: run,
}

func run(cmd *cobra.Command, argv []string) {
fmt.Fprintf(os.Stdout, "%s\n", info.Version)
}
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module gitlab.cee.redhat.com/service/moactl

go 1.13

require (
github.com/openshift-online/ocm-sdk-go v0.1.90
github.com/spf13/cobra v0.0.6
github.com/spf13/pflag v1.0.5
)
Loading

0 comments on commit 357b274

Please sign in to comment.