本文整理汇总了Golang中github.com/openshift/origin/pkg/cmd/util/clientcmd.IsForbidden函数的典型用法代码示例。如果您正苦于以下问题:Golang IsForbidden函数的具体用法?Golang IsForbidden怎么用?Golang IsForbidden使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了IsForbidden函数的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: RunProjects
// RunProjects lists all projects a user belongs to
func (o ProjectsOptions) RunProjects() error {
config := o.Config
clientCfg := o.ClientConfig
out := o.Out
var currentProject string
currentContext := config.Contexts[config.CurrentContext]
if currentContext != nil {
currentProject = currentContext.Namespace
}
var currentProjectExists bool
var currentProjectErr error
client := o.Client
if len(currentProject) > 0 {
if _, currentProjectErr := client.Projects().Get(currentProject); currentProjectErr == nil {
currentProjectExists = true
}
}
var defaultContextName string
if currentContext != nil {
defaultContextName = cliconfig.GetContextNickname(currentContext.Namespace, currentContext.Cluster, currentContext.AuthInfo)
}
var msg string
projects, err := getProjects(client)
if err == nil {
switch len(projects) {
case 0:
msg += "You are not a member of any projects. You can request a project to be created with the 'new-project' command."
case 1:
if o.DisplayShort {
msg += fmt.Sprintf("%s", api.DisplayNameAndNameForProject(&projects[0]))
} else {
msg += fmt.Sprintf("You have one project on this server: %q.", api.DisplayNameAndNameForProject(&projects[0]))
}
default:
asterisk := ""
count := 0
if !o.DisplayShort {
msg += fmt.Sprintf("You have access to the following projects and can switch between them with '%s project <projectname>':\n", o.CommandName)
}
sort.Sort(SortByProjectName(projects))
for _, project := range projects {
count = count + 1
displayName := project.Annotations["openshift.io/display-name"]
linebreak := "\n"
if len(displayName) == 0 {
displayName = project.Annotations["displayName"]
}
if currentProjectExists && !o.DisplayShort {
asterisk = " "
if currentProject == project.Name {
asterisk = " * "
}
}
if len(displayName) > 0 && displayName != project.Name && !o.DisplayShort {
msg += fmt.Sprintf("\n"+asterisk+"%s - %s", project.Name, displayName)
} else {
if o.DisplayShort && count == 1 {
linebreak = ""
}
msg += fmt.Sprintf(linebreak+asterisk+"%s", project.Name)
}
}
}
fmt.Println(msg)
if len(projects) > 0 && !o.DisplayShort {
if !currentProjectExists {
if clientcmd.IsForbidden(currentProjectErr) {
fmt.Printf("You do not have rights to view project %q. Please switch to an existing one.\n", currentProject)
}
return currentProjectErr
}
// if they specified a project name and got a generated context, then only show the information they care about. They won't recognize
// a context name they didn't choose
if config.CurrentContext == defaultContextName {
fmt.Fprintf(out, "\nUsing project %q on server %q.\n", currentProject, clientCfg.Host)
} else {
fmt.Fprintf(out, "\nUsing project %q from context named %q on server %q.\n", currentProject, config.CurrentContext, clientCfg.Host)
}
}
return nil
}
return err
}
示例2: RunProject
// RunProject contains all the necessary functionality for the OpenShift cli project command
func (o ProjectOptions) RunProject() error {
config := o.Config
clientCfg := o.ClientConfig
out := o.Out
// No argument provided, we will just print info
if len(o.ProjectName) == 0 {
currentContext := config.Contexts[config.CurrentContext]
currentProject := currentContext.Namespace
if len(currentProject) > 0 {
if o.DisplayShort {
fmt.Fprintln(out, currentProject)
return nil
}
_, err := o.Client.Projects().Get(currentProject)
if err != nil {
if kapierrors.IsNotFound(err) {
return fmt.Errorf("the project %q specified in your config does not exist.", currentProject)
}
if clientcmd.IsForbidden(err) {
return fmt.Errorf("you do not have rights to view project %q.", currentProject)
}
return err
}
if config.CurrentContext != currentProject {
if len(currentProject) > 0 {
fmt.Fprintf(out, "Using project %q from context named %q on server %q.\n", currentProject, config.CurrentContext, clientCfg.Host)
} else {
fmt.Fprintf(out, "Using context named %q on server %q.\n", config.CurrentContext, clientCfg.Host)
}
} else {
fmt.Fprintf(out, "Using project %q on server %q.\n", currentProject, clientCfg.Host)
}
} else {
if o.DisplayShort {
return fmt.Errorf("no project has been set")
}
fmt.Fprintf(out, "No project has been set. Pass a project name to make that the default.\n")
}
return nil
}
// We have an argument that can be either a context or project
argument := o.ProjectName
contextInUse := ""
namespaceInUse := ""
contextNameIsGenerated := false
// Check if argument is an existing context, if so just set it as the context in use.
// If not a context then we will try to handle it as a project.
if context, contextExists := config.Contexts[argument]; !o.ProjectOnly && contextExists {
contextInUse = argument
namespaceInUse = context.Namespace
config.CurrentContext = argument
} else {
if !o.SkipAccessValidation {
_, err := o.Client.Projects().Get(argument)
if err != nil {
if isNotFound, isForbidden := kapierrors.IsNotFound(err), clientcmd.IsForbidden(err); isNotFound || isForbidden {
var msg string
if isForbidden {
msg = fmt.Sprintf("You are not a member of project %q.", argument)
} else {
msg = fmt.Sprintf("A project named %q does not exist on %q.", argument, clientCfg.Host)
}
projects, err := getProjects(o.Client)
if err == nil {
switch len(projects) {
case 0:
msg += "\nYou are not a member of any projects. You can request a project to be created with the 'new-project' command."
case 1:
msg += fmt.Sprintf("\nYou have one project on this server: %s", api.DisplayNameAndNameForProject(&projects[0]))
default:
msg += "\nYour projects are:"
for _, project := range projects {
msg += fmt.Sprintf("\n* %s", api.DisplayNameAndNameForProject(&project))
}
}
}
if hasMultipleServers(config) {
msg += "\nTo see projects on another server, pass '--server=<server>'."
}
return errors.New(msg)
}
return err
}
}
projectName := argument
kubeconfig, err := cliconfig.CreateConfig(projectName, o.ClientConfig)
//.........这里部分代码省略.........
示例3: gatherProjectInfo
// Discover the projects available for the established session and take one to use. It
// fails in case of no existing projects, and print out useful information in case of
// multiple projects.
// Requires o.Username to be set.
func (o *LoginOptions) gatherProjectInfo() error {
me, err := o.whoAmI()
if err != nil {
return err
}
if o.Username != me.Name {
return fmt.Errorf("current user, %v, does not match expected user %v", me.Name, o.Username)
}
oClient, err := client.New(o.Config)
if err != nil {
return err
}
projects, err := oClient.Projects().List(labels.Everything(), fields.Everything())
if err != nil {
return err
}
projectsItems := projects.Items
switch len(projectsItems) {
case 0:
fmt.Fprintf(o.Out, `You don't have any projects. You can try to create a new project, by running
$ oc new-project <projectname>
`)
o.Project = o.DefaultNamespace
case 1:
o.Project = projectsItems[0].Name
fmt.Fprintf(o.Out, "Using project %q.\n", o.Project)
default:
projects := util.StringSet{}
for _, project := range projectsItems {
projects.Insert(project.Name)
}
namespace := o.DefaultNamespace
if !projects.Has(namespace) {
if namespace != kapi.NamespaceDefault && projects.Has(kapi.NamespaceDefault) {
namespace = kapi.NamespaceDefault
} else {
namespace = projects.List()[0]
}
}
if current, err := oClient.Projects().Get(namespace); err == nil {
o.Project = current.Name
fmt.Fprintf(o.Out, "Using project %q.\n", o.Project)
} else if !kerrors.IsNotFound(err) && !clientcmd.IsForbidden(err) {
return err
}
fmt.Fprintf(o.Out, "\nYou have access to the following projects and can switch between them with 'oc project <projectname>':\n\n")
for _, p := range projects.List() {
if o.Project == p {
fmt.Fprintf(o.Out, " * %s (current)\n", p)
} else {
fmt.Fprintf(o.Out, " * %s\n", p)
}
}
fmt.Fprintln(o.Out)
}
return nil
}
示例4: gatherProjectInfo
// Discover the projects available for the established session and take one to use. It
// fails in case of no existing projects, and print out useful information in case of
// multiple projects.
// Requires o.Username to be set.
func (o *LoginOptions) gatherProjectInfo() error {
me, err := o.whoAmI()
if err != nil {
return err
}
if o.Username != me.Name {
return fmt.Errorf("current user, %v, does not match expected user %v", me.Name, o.Username)
}
oClient, err := client.New(o.Config)
if err != nil {
return err
}
projectsList, err := oClient.Projects().List(kapi.ListOptions{})
// if we're running on kube (or likely kube), just set it to "default"
if kerrors.IsNotFound(err) || kerrors.IsForbidden(err) {
fmt.Fprintf(o.Out, "Using \"default\". You can switch projects with '%s project <projectname>':\n\n", o.CommandName)
o.Project = "default"
return nil
}
if err != nil {
return err
}
projectsItems := projectsList.Items
projects := sets.String{}
for _, project := range projectsItems {
projects.Insert(project.Name)
}
if len(o.DefaultNamespace) > 0 && !projects.Has(o.DefaultNamespace) {
// Attempt a direct get of our current project in case it hasn't appeared in the list yet
if currentProject, err := oClient.Projects().Get(o.DefaultNamespace); err == nil {
// If we get it successfully, add it to the list
projectsItems = append(projectsItems, *currentProject)
projects.Insert(currentProject.Name)
}
}
switch len(projectsItems) {
case 0:
canRequest, err := o.canRequestProjects()
if err != nil {
return err
}
if !canRequest {
fmt.Fprintf(o.Out, "You do not have access to create new projects, contact your system administrator to request a project.\n")
return nil
}
fmt.Fprintf(o.Out, `You don't have any projects. You can try to create a new project, by running
%s new-project <projectname>
`, o.CommandName)
o.Project = ""
case 1:
o.Project = projectsItems[0].Name
fmt.Fprintf(o.Out, "You have one project on this server: %q\n\n", o.Project)
fmt.Fprintf(o.Out, "Using project %q.\n", o.Project)
default:
namespace := o.DefaultNamespace
if !projects.Has(namespace) {
if namespace != kapi.NamespaceDefault && projects.Has(kapi.NamespaceDefault) {
namespace = kapi.NamespaceDefault
} else {
namespace = projects.List()[0]
}
}
current, err := oClient.Projects().Get(namespace)
if err != nil && !kerrors.IsNotFound(err) && !clientcmd.IsForbidden(err) {
return err
}
o.Project = current.Name
fmt.Fprintf(o.Out, "You have access to the following projects and can switch between them with '%s project <projectname>':\n\n", o.CommandName)
for _, p := range projects.List() {
if o.Project == p {
fmt.Fprintf(o.Out, " * %s\n", p)
} else {
fmt.Fprintf(o.Out, " %s\n", p)
}
}
fmt.Fprintln(o.Out)
fmt.Fprintf(o.Out, "Using project %q.\n", o.Project)
}
return nil
}
示例5: RunProject
// RunProject contains all the necessary functionality for the OpenShift cli project command
func (o ProjectOptions) RunProject() error {
config := o.Config
clientCfg := o.ClientConfig
out := o.Out
// No argument provided, we will just print info
if len(o.ProjectName) == 0 {
currentContext := config.Contexts[config.CurrentContext]
currentProject := currentContext.Namespace
if len(currentProject) > 0 {
if o.DisplayShort {
fmt.Fprintln(out, currentProject)
return nil
}
_, err := o.Client.Projects().Get(currentProject)
if err != nil {
if kapierrors.IsNotFound(err) {
return fmt.Errorf("the project %q specified in your config does not exist.", currentProject)
}
if clientcmd.IsForbidden(err) {
return fmt.Errorf("you do not have rights to view project %q.", currentProject)
}
return err
}
defaultContextName := cliconfig.GetContextNickname(currentContext.Namespace, currentContext.Cluster, currentContext.AuthInfo)
// if they specified a project name and got a generated context, then only show the information they care about. They won't recognize
// a context name they didn't choose
if config.CurrentContext == defaultContextName {
fmt.Fprintf(out, "Using project %q on server %q.\n", currentProject, clientCfg.Host)
} else {
fmt.Fprintf(out, "Using project %q from context named %q on server %q.\n", currentProject, config.CurrentContext, clientCfg.Host)
}
} else {
if o.DisplayShort {
return fmt.Errorf("no project has been set")
}
fmt.Fprintf(out, "No project has been set. Pass a project name to make that the default.\n")
}
return nil
}
// We have an argument that can be either a context or project
argument := o.ProjectName
contextInUse := ""
namespaceInUse := ""
// Check if argument is an existing context, if so just set it as the context in use.
// If not a context then we will try to handle it as a project.
if context, contextExists := config.Contexts[argument]; !o.ProjectOnly && contextExists {
contextInUse = argument
namespaceInUse = context.Namespace
config.CurrentContext = argument
} else {
if !o.SkipAccessValidation {
_, err := o.Client.Projects().Get(argument)
if err != nil {
if isNotFound, isForbidden := kapierrors.IsNotFound(err), clientcmd.IsForbidden(err); isNotFound || isForbidden {
var msg string
if isForbidden {
msg = fmt.Sprintf("You are not a member of project %q.", argument)
} else {
msg = fmt.Sprintf("A project named %q does not exist on %q.", argument, clientCfg.Host)
}
projects, err := getProjects(o.Client)
if err == nil {
switch len(projects) {
case 0:
msg += "\nYou are not a member of any projects. You can request a project to be created with the 'new-project' command."
case 1:
msg += fmt.Sprintf("\nYou have one project on this server: %s", api.DisplayNameAndNameForProject(&projects[0]))
default:
msg += "\nYour projects are:"
for _, project := range projects {
msg += fmt.Sprintf("\n* %s", api.DisplayNameAndNameForProject(&project))
}
}
}
if hasMultipleServers(config) {
msg += "\nTo see projects on another server, pass '--server=<server>'."
}
return errors.New(msg)
}
return err
}
}
projectName := argument
kubeconfig, err := cliconfig.CreateConfig(projectName, o.ClientConfig)
//.........这里部分代码省略.........
示例6: gatherProjectInfo
// Discover the projects available for the established session and take one to use. It
// fails in case of no existing projects, and print out useful information in case of
// multiple projects.
// Requires o.Username to be set.
func (o *LoginOptions) gatherProjectInfo() error {
me, err := o.whoAmI()
if err != nil {
return err
}
if o.Username != me.Name {
return fmt.Errorf("current user, %v, does not match expected user %v", me.Name, o.Username)
}
oClient, err := client.New(o.Config)
if err != nil {
return err
}
projects, err := oClient.Projects().List(kapi.ListOptions{})
if err != nil {
return err
}
projectsItems := projects.Items
switch len(projectsItems) {
case 0:
fmt.Fprintf(o.Out, `You don't have any projects. You can try to create a new project, by running
%s new-project <projectname>
`, o.CommandName)
o.Project = ""
case 1:
o.Project = projectsItems[0].Name
fmt.Fprintf(o.Out, "You have one project on this server: %q\n\n", o.Project)
fmt.Fprintf(o.Out, "Using project %q.\n", o.Project)
default:
projects := sets.String{}
for _, project := range projectsItems {
projects.Insert(project.Name)
}
namespace := o.DefaultNamespace
if !projects.Has(namespace) {
if namespace != kapi.NamespaceDefault && projects.Has(kapi.NamespaceDefault) {
namespace = kapi.NamespaceDefault
} else {
namespace = projects.List()[0]
}
}
current, err := oClient.Projects().Get(namespace)
if err != nil && !kerrors.IsNotFound(err) && !clientcmd.IsForbidden(err) {
return err
}
o.Project = current.Name
fmt.Fprintf(o.Out, "You have access to the following projects and can switch between them with '%s project <projectname>':\n\n", o.CommandName)
for _, p := range projects.List() {
if o.Project == p {
fmt.Fprintf(o.Out, " * %s\n", p)
} else {
fmt.Fprintf(o.Out, " %s\n", p)
}
}
fmt.Fprintln(o.Out)
fmt.Fprintf(o.Out, "Using project %q.\n", o.Project)
}
return nil
}