Skip to content

Commit

Permalink
feature: bcs-client supports bcs-user-manager api. issue TencentBlueK…
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanhe-bupt committed Apr 23, 2020
1 parent 9ae389c commit 55aed44
Show file tree
Hide file tree
Showing 17 changed files with 773 additions and 7 deletions.
67 changes: 67 additions & 0 deletions bcs-services/bcs-client/cmd/add/cidr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Tencent is pleased to support the open source community by making Blueking Container Service available.
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* 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 add

import (
"bk-bcs/bcs-services/bcs-client/cmd/utils"
v1 "bk-bcs/bcs-services/bcs-client/pkg/usermanager/v1"
"bk-bcs/bcs-services/bcs-user-manager/app/user-manager/v1http"
"encoding/json"
"fmt"
"io/ioutil"
"os"
)

func initVpcCidr(c *utils.ClientContext) error {
if err := c.MustSpecified(utils.OptionVpc); err != nil {
return err
}

var data []byte
var err error
if !c.IsSet(utils.OptionFile) {
//reading all data from stdin
data, err = ioutil.ReadAll(os.Stdin)
} else {
data, err = c.FileData()
}
if err != nil {
return err
}
if len(data) == 0 {
return fmt.Errorf("failed to grant: no available resource datas")
}

var cidrs []v1http.TkeCidr
err = json.Unmarshal(data, &cidrs)
if err != nil {
return err
}
form := v1http.AddTkeCidrForm{
Vpc: c.String(utils.OptionVpc),
TkeCidrs: cidrs,
}
data, err = json.Marshal(form)
if err != nil {
return err
}
userManager := v1.NewBcsUserManager(utils.GetClientOption())
err = userManager.AddVpcCidrs(data)
if err != nil {
return fmt.Errorf("failed to init cidr to vpc %s: %v", c.String(utils.OptionVpc), err)
}

fmt.Printf("success to init cidr\n")
return nil
}
60 changes: 60 additions & 0 deletions bcs-services/bcs-client/cmd/add/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Tencent is pleased to support the open source community by making Blueking Container Service available.
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* 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 add

import (
"bk-bcs/bcs-services/bcs-client/cmd/utils"
"fmt"
"github.com/urfave/cli"
)

//NewAddCommand sub command add registration
func NewAddCommand() cli.Command {
return cli.Command{
Name: "add",
Usage: "add cidr",
Flags: []cli.Flag{
cli.StringFlag{
Name: "type, t",
Usage: "add type, value can be cidr",
},
cli.StringFlag{
Name: "from-file, f",
Usage: "reading with configuration `FILE`",
},
cli.StringFlag{
Name: "vpcid",
Usage: "vpc id",
},
},
Action: func(c *cli.Context) error {
return add(utils.NewClientContext(c))
},
}
}

func add(c *utils.ClientContext) error {
if err := c.MustSpecified(utils.OptionType); err != nil {
return err
}

resourceType := c.String(utils.OptionType)

switch resourceType {
case "cidr":
return initVpcCidr(c)
default:
return fmt.Errorf("invalid type: %s", resourceType)
}
}
14 changes: 12 additions & 2 deletions bcs-services/bcs-client/cmd/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
func NewCreateCommand() cli.Command {
return cli.Command{
Name: "create",
Usage: "create new application/process/service/secret/configmap/deployment",
Usage: "create new application/process/service/secret/configmap/deployment/user",
Flags: []cli.Flag{
cli.StringFlag{
Name: "from-file, f",
Expand All @@ -35,7 +35,15 @@ func NewCreateCommand() cli.Command {
},
cli.StringFlag{
Name: "type, t",
Usage: "Create type, value can be app/service/secret/configmap/deployment",
Usage: "Create type, value can be app/service/secret/configmap/deployment/user",
},
cli.StringFlag{
Name: "usertype",
Usage: "user type, value can be admin/saas/plain",
},
cli.StringFlag{
Name: "username",
Usage: "user name",
},
},
Action: func(c *cli.Context) error {
Expand Down Expand Up @@ -66,6 +74,8 @@ func create(c *utils.ClientContext) error {
return createDeployment(c)
case "crd", "customresourcedefinition":
return createCustomResourceDefinition(c)
case "user":
return createUser(c)
default:
//unkown type, try CustomResource
return createCustomResource(c)
Expand Down
41 changes: 41 additions & 0 deletions bcs-services/bcs-client/cmd/create/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Tencent is pleased to support the open source community by making Blueking Container Service available.
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* 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"
"net/http"

"bk-bcs/bcs-services/bcs-client/cmd/utils"
"bk-bcs/bcs-services/bcs-client/pkg/usermanager/v1"
)

func createUser(c *utils.ClientContext) error {
if err := c.MustSpecified(utils.OptionUserName, utils.OptionUserType); err != nil {
return err
}

userManager := v1.NewBcsUserManager(utils.GetClientOption())
user, err := userManager.CreateOrGetUser(c.String(utils.OptionUserType), c.String(utils.OptionUserName), http.MethodPost)
if err != nil {
return fmt.Errorf("failed to create user: %v", err)
}

return printResult(user)
}

func printResult(single interface{}) error {
fmt.Printf("%s\n", utils.TryIndent(single))
return nil
}
63 changes: 59 additions & 4 deletions bcs-services/bcs-client/cmd/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,26 @@
package get

import (
"fmt"

"bk-bcs/bcs-services/bcs-client/cmd/utils"
"bk-bcs/bcs-services/bcs-client/pkg/scheduler/v4"
"bk-bcs/bcs-services/bcs-client/pkg/storage/v1"
userV1 "bk-bcs/bcs-services/bcs-client/pkg/usermanager/v1"
"bk-bcs/bcs-services/bcs-user-manager/app/user-manager/v1http"
"encoding/json"
"fmt"
"net/http"

"github.com/urfave/cli"
)

func NewGetCommand() cli.Command {
return cli.Command{
Name: "get",
Usage: "get the original definition of application/process/deployment/ippoolstatic/ippoolstatic-detail",
Usage: "get the original definition of application/process/deployment/ippoolstatic/ippoolstatic-detail/user/permission",
Flags: []cli.Flag{
cli.StringFlag{
Name: "type, t",
Usage: "Get type, application(app)/process/deployment(deploy)/ippoolstatic(ipps)/ippoolstatic-detail(ippsd)",
Usage: "Get type, application(app)/process/deployment(deploy)/ippoolstatic(ipps)/ippoolstatic-detail(ippsd)/user/permission",
},
cli.StringFlag{
Name: "clusterid",
Expand All @@ -45,6 +48,18 @@ func NewGetCommand() cli.Command {
Name: "name, n",
Usage: "Name",
},
cli.StringFlag{
Name: "usertype",
Usage: "user type, value can be admin/saas/plain",
},
cli.StringFlag{
Name: "username",
Usage: "user name",
},
cli.StringFlag{
Name: "resourcetype",
Usage: "resource type, value can be cluster/storage/network-detection...",
},
},
Action: func(c *cli.Context) error {
if err := get(utils.NewClientContext(c)); err != nil {
Expand Down Expand Up @@ -73,6 +88,10 @@ func get(c *utils.ClientContext) error {
return getIPPoolStatic(c)
case "ippsd", "ippoolstatic-detail":
return getIPPoolStaticDetail(c)
case "user":
return getUser(c)
case "permission":
return getPermission(c)
default:
return fmt.Errorf("invalid type: %s", resourceType)
}
Expand Down Expand Up @@ -158,6 +177,42 @@ func getIPPoolStaticDetail(c *utils.ClientContext) error {
return printGet(result[0].Data)
}

func getUser(c *utils.ClientContext) error {
if err := c.MustSpecified(utils.OptionUserName, utils.OptionUserType); err != nil {
return err
}

userManager := userV1.NewBcsUserManager(utils.GetClientOption())
user, err := userManager.CreateOrGetUser(c.String(utils.OptionUserType), c.String(utils.OptionUserName), http.MethodGet)
if err != nil {
return fmt.Errorf("failed to create user: %v", err)
}

return printGet(user)
}

func getPermission(c *utils.ClientContext) error {
if err := c.MustSpecified(utils.OptionUserName, utils.OptionResourceType); err != nil {
return err
}

userManager := userV1.NewBcsUserManager(utils.GetClientOption())
pf := v1http.GetPermissionForm{
UserName: c.String(utils.OptionUserName),
ResourceType: c.String(utils.OptionResourceType),
}
data, err := json.Marshal(pf)
if err != nil {
return err
}
permissions, err := userManager.ActPermission(http.MethodGet, data)
if err != nil {
return fmt.Errorf("failed to grant permission: %v", err)
}

return printGet(permissions)
}

func printGet(single interface{}) error {
fmt.Printf("%s\n", utils.TryIndent(single))
return nil
Expand Down
56 changes: 56 additions & 0 deletions bcs-services/bcs-client/cmd/grant/grant.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Tencent is pleased to support the open source community by making Blueking Container Service available.
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* 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 grant

import (
"bk-bcs/bcs-services/bcs-client/cmd/utils"
"fmt"
"github.com/urfave/cli"
)

//NewGrantCommand sub command grant registration
func NewGrantCommand() cli.Command {
return cli.Command{
Name: "grant",
Usage: "grant permission",
Flags: []cli.Flag{
cli.StringFlag{
Name: "type, t",
Usage: "grant type, value can be permission",
},
cli.StringFlag{
Name: "from-file, f",
Usage: "reading with configuration `FILE`",
},
},
Action: func(c *cli.Context) error {
return grant(utils.NewClientContext(c))
},
}
}

func grant(c *utils.ClientContext) error {
if err := c.MustSpecified(utils.OptionType); err != nil {
return err
}

resourceType := c.String(utils.OptionType)

switch resourceType {
case "permission":
return grantPermission(c)
default:
return fmt.Errorf("invalid type: %s", resourceType)
}
}
Loading

0 comments on commit 55aed44

Please sign in to comment.