github.com/vmware/govmomi@v0.37.2/vapi/rest/example_test.go (about)

     1  /*
     2  Copyright (c) 2023-2023 VMware, Inc. All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package rest_test
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"net/url"
    23  
    24  	_ "github.com/vmware/govmomi/lookup/simulator"
    25  	"github.com/vmware/govmomi/simulator"
    26  	"github.com/vmware/govmomi/sts"
    27  	_ "github.com/vmware/govmomi/sts/simulator"
    28  	"github.com/vmware/govmomi/vapi/rest"
    29  	"github.com/vmware/govmomi/vim25"
    30  )
    31  
    32  func ExampleClient_LoginByToken() {
    33  	simulator.Run(func(ctx context.Context, vc *vim25.Client) error {
    34  		c, err := sts.NewClient(ctx, vc)
    35  		if err != nil {
    36  			return err
    37  		}
    38  
    39  		// Issue a bearer token
    40  		req := sts.TokenRequest{
    41  			Userinfo: url.UserPassword("Administrator@VSPHERE.LOCAL", "password"),
    42  		}
    43  
    44  		signer, err := c.Issue(ctx, req)
    45  		if err != nil {
    46  			return err
    47  		}
    48  
    49  		rc := rest.NewClient(vc)
    50  
    51  		err = rc.LoginByToken(rc.WithSigner(ctx, signer))
    52  		if err != nil {
    53  			return err
    54  		}
    55  
    56  		session, err := rc.Session(ctx)
    57  		if err != nil {
    58  			return err
    59  		}
    60  
    61  		// Note: vcsim does not currently parse the token NameID for rest as it does for soap
    62  		fmt.Println(session.User)
    63  
    64  		return nil
    65  	})
    66  	// Output: TODO
    67  }