Esempio n. 1
0
// NewControllerAPIClient returns an API client for the Controller on
// the current controller using the current credentials.
func (c *ControllerCommandBase) NewControllerAPIClient() (*controller.Client, error) {
	root, err := c.NewAPIRoot()
	if err != nil {
		return nil, errors.Trace(err)
	}
	return controller.NewClient(root), nil
}
Esempio n. 2
0
File: list.go Progetto: bac/juju
// getControllerAPI returns a block api for block manipulation.
func getControllerAPI(c newControllerAPIRoot) (*controller.Client, error) {
	root, err := c.NewControllerAPIRoot()
	if err != nil {
		return nil, errors.Trace(err)
	}
	return controller.NewClient(root), nil
}
Esempio n. 3
0
File: gui_test.go Progetto: bac/juju
// withHTTPClient sets up a fixture with the given address and handle, then
// runs the given test and checks that the HTTP handler has been called with
// the given method.
func withHTTPClient(c *gc.C, address, expectMethod string, handle func(http.ResponseWriter, *http.Request), test func(*controller.Client)) {
	fix := newHTTPFixture(address, handle)
	stub := fix.run(c, func(ac base.APICallCloser) {
		client := controller.NewClient(ac)
		test(client)
	})
	stub.CheckCalls(c, []testing.StubCall{{expectMethod, nil}})
}
Esempio n. 4
0
func (s *Suite) TestHostedModelConfigs_FormatResults(c *gc.C) {
	apiCaller := apitesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
		c.Assert(objType, gc.Equals, "Controller")
		c.Assert(request, gc.Equals, "HostedModelConfigs")
		c.Assert(arg, gc.IsNil)
		out := result.(*params.HostedModelConfigsResults)
		c.Assert(out, gc.NotNil)
		*out = params.HostedModelConfigsResults{
			Models: []params.HostedModelConfig{
				{
					Name:     "first",
					OwnerTag: "user-foo@bar",
					Config: map[string]interface{}{
						"name": "first",
					},
					CloudSpec: &params.CloudSpec{
						Type: "magic",
						Name: "first",
					},
				}, {
					Name:     "second",
					OwnerTag: "bad-tag",
				}, {
					Name:     "third",
					OwnerTag: "user-foo@bar",
					Config: map[string]interface{}{
						"name": "third",
					},
					CloudSpec: &params.CloudSpec{
						Name: "third",
					},
				},
			},
		}
		return nil
	})
	client := controller.NewClient(apiCaller)
	config, err := client.HostedModelConfigs()
	c.Assert(config, gc.HasLen, 3)
	c.Assert(err, jc.ErrorIsNil)
	first := config[0]
	c.Assert(first.Name, gc.Equals, "first")
	c.Assert(first.Owner, gc.Equals, names.NewUserTag("foo@bar"))
	c.Assert(first.Config, gc.DeepEquals, map[string]interface{}{
		"name": "first",
	})
	c.Assert(first.CloudSpec, gc.DeepEquals, environs.CloudSpec{
		Type: "magic",
		Name: "first",
	})
	second := config[1]
	c.Assert(second.Name, gc.Equals, "second")
	c.Assert(second.Error.Error(), gc.Equals, `"bad-tag" is not a valid tag`)
	third := config[2]
	c.Assert(third.Name, gc.Equals, "third")
	c.Assert(third.Error.Error(), gc.Equals, "validating CloudSpec: empty Type not valid")
}
Esempio n. 5
0
func (s *Suite) TestHostedModelConfigs_CallError(c *gc.C) {
	apiCaller := apitesting.APICallerFunc(func(string, int, string, string, interface{}, interface{}) error {
		return errors.New("boom")
	})
	client := controller.NewClient(apiCaller)
	config, err := client.HostedModelConfigs()
	c.Check(config, gc.HasLen, 0)
	c.Check(err, gc.ErrorMatches, "boom")
}
Esempio n. 6
0
func (s *Suite) TestInitiateMigrationCallError(c *gc.C) {
	apiCaller := apitesting.APICallerFunc(func(string, int, string, string, interface{}, interface{}) error {
		return errors.New("boom")
	})
	client := controller.NewClient(apiCaller)
	id, err := client.InitiateMigration(makeSpec())
	c.Check(id, gc.Equals, "")
	c.Check(err, gc.ErrorMatches, "boom")
}
Esempio n. 7
0
func (c *destroyCommandBase) getControllerAPI() (destroyControllerAPI, error) {
	if c.api != nil {
		return c.api, c.apierr
	}
	root, err := c.NewAPIRoot()
	if err != nil {
		return nil, errors.Trace(err)
	}
	return controller.NewClient(root), nil
}
Esempio n. 8
0
func (c *showControllerCommand) getAPI(controllerName string) (ControllerAccessAPI, error) {
	if c.api != nil {
		return c.api(controllerName), nil
	}
	api, err := c.NewAPIRoot(c.store, controllerName, "")
	if err != nil {
		return nil, errors.Annotate(err, "opening API connection")
	}
	return controller.NewClient(api), nil
}
Esempio n. 9
0
func (c *getConfigCommand) getAPI() (controllerAPI, error) {
	if c.api != nil {
		return c.api, nil
	}
	root, err := c.NewAPIRoot()
	if err != nil {
		return nil, errors.Trace(err)
	}
	return apicontroller.NewClient(root), nil
}
Esempio n. 10
0
func makeClient(results params.InitiateMigrationResults) (
	*controller.Client, *jujutesting.Stub,
) {
	var stub jujutesting.Stub
	apiCaller := apitesting.APICallerFunc(
		func(objType string, version int, id, request string, arg, result interface{}) error {
			stub.AddCall(objType+"."+request, arg)
			out := result.(*params.InitiateMigrationResults)
			*out = results
			return nil
		},
	)
	client := controller.NewClient(apiCaller)
	return client, &stub
}
Esempio n. 11
0
func main() {
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "jujuwatchall [flags] [<controller>|<model>|[<controller>]:[<model>]]\n")
		fmt.Fprintf(os.Stderr, `
jujuwatchall -a
	- watches all models on the current controller

jujuwatchall :modelname
	- watches the named model on the current controller.
jujuwatchall :
	- watches the current model on the current controller
jujuwatchall controllername:
	- watches all models on the named controller.
jujuwatchall controllername:modelname
	- watches the named model on the named controller
`)
		os.Exit(2)
	}
	flag.Parse()
	if *jsonFlag {
		json.MarshalIndent = stdjson.MarshalIndent
		json.Marshal = stdjson.Marshal
		json.Unmarshal = stdjson.Unmarshal
	}
	controllerName, modelName := "", ""
	if flag.NArg() > 0 {
		split := strings.Split(flag.Arg(0), ":")
		switch len(split) {
		case 1:
			if *allFlag {
				controllerName = split[0]
			} else {
				modelName = split[0]
			}
		case 2:
			controllerName, modelName = split[0], split[1]
		default:
			flag.Usage()
		}
	}
	var w *api.AllWatcher
	if *allFlag {
		conn, err := jujuconn.DialController(controllerName)
		if err != nil {
			log.Fatalf("cannot dial controller: %v", err)
		}
		w, err = apicontroller.NewClient(conn).WatchAllModels()
		if err != nil {
			log.Fatalf("cannot watch all models: %v", err)
		}
	} else {
		conn, err := jujuconn.DialModel(controllerName, modelName)
		if err != nil {
			log.Fatalf("cannot dial model: %v", err)
		}
		w, err = conn.Client().WatchAll()
		if err != nil {
			log.Fatalf("cannot watch models: %v", err)
		}
	}
	entities := make(map[multiwatcher.EntityId]map[string]interface{})
	for {
		deltas, err := w.Next()
		if err != nil {
			log.Fatalf("Next error: %v", err)
		}
		for _, d := range deltas {
			id := d.Entity.EntityId()
			if d.Removed {
				fmt.Printf("- %s %v\n", id.Kind, id.Id)
				delete(entities, id)
				continue
			}
			oldFields := entities[id]
			if oldFields == nil {
				data, _ := json.MarshalIndent(d.Entity, "", "\t")
				var fields map[string]interface{}
				if err := json.Unmarshal(data, &fields); err != nil {
					panic("cannot re-unmrshal json")
				}
				entities[id] = fields
				fmt.Printf("+ %s %v %v %s\n", id.Kind, id.Id, id.ModelUUID, data)
				continue
			}
			data, _ := json.Marshal(d.Entity)
			var fields map[string]interface{}
			if err := json.Unmarshal(data, &fields); err != nil {
				panic("cannot re-unmrshal json")
			}
			names := make(map[string]bool)
			for name := range oldFields {
				names[name] = true
			}
			for name := range fields {
				names[name] = true
			}
			changedFields := make(map[string]interface{})
			for name := range names {
				if !reflect.DeepEqual(fields[name], oldFields[name]) {
					changedFields[name] = fields[name]
				}
			}
			entities[id] = fields
			data, _ = json.MarshalIndent(changedFields, "", "\t")
			fmt.Printf("| %s %v %v %s\n", id.Kind, id.Id, id.ModelUUID, data)
		}
		fmt.Printf("\n")
	}
}
Esempio n. 12
0
func (s *controllerSuite) OpenAPI(c *gc.C) *controller.Client {
	conn, err := juju.NewAPIState(s.AdminUserTag(c), s.Environ, api.DialOpts{})
	c.Assert(err, jc.ErrorIsNil)
	s.AddCleanup(func(*gc.C) { conn.Close() })
	return controller.NewClient(conn)
}
Esempio n. 13
0
}

var getModelConfigAPI = func(c *upgradeJujuCommand) (modelConfigAPI, error) {
	api, err := c.NewAPIRoot()
	if err != nil {
		return nil, errors.Trace(err)
	}
	return modelconfig.NewClient(api), nil
}

var getControllerAPI = func(c *upgradeJujuCommand) (controllerAPI, error) {
	api, err := c.NewControllerAPIRoot()
	if err != nil {
		return nil, errors.Trace(err)
	}
	return controller.NewClient(api), nil
}

// Run changes the version proposed for the juju envtools.
func (c *upgradeJujuCommand) Run(ctx *cmd.Context) (err error) {

	client, err := getUpgradeJujuAPI(c)
	if err != nil {
		return err
	}
	defer client.Close()
	modelConfigClient, err := getModelConfigAPI(c)
	if err != nil {
		return err
	}
	defer modelConfigClient.Close()
Esempio n. 14
0
func (s *controllerSuite) OpenAPI(c *gc.C) *controller.Client {
	return controller.NewClient(s.APIState)
}
Esempio n. 15
0
func (s *legacySuite) OpenAPI(c *gc.C) *controller.Client {
	return controller.NewClient(s.OpenControllerAPI(c))
}
Esempio n. 16
0
func (s *legacySuite) TestAPIServerCanShutdownWithOutstandingNext(c *gc.C) {

	lis, err := net.Listen("tcp", "localhost:0")
	c.Assert(err, jc.ErrorIsNil)

	srv, err := apiserver.NewServer(s.State, lis, apiserver.ServerConfig{
		Clock:       clock.WallClock,
		Cert:        []byte(testing.ServerCert),
		Key:         []byte(testing.ServerKey),
		Tag:         names.NewMachineTag("0"),
		DataDir:     c.MkDir(),
		LogDir:      c.MkDir(),
		NewObserver: func() observer.Observer { return &fakeobserver.Instance{} },
	})
	c.Assert(err, gc.IsNil)

	// Connect to the API server we've just started.
	apiInfo := s.APIInfo(c)
	apiInfo.Addrs = []string{lis.Addr().String()}
	apiInfo.ModelTag = names.ModelTag{}
	apiState, err := api.Open(apiInfo, api.DialOpts{})
	sysManager := controller.NewClient(apiState)
	defer sysManager.Close()

	w, err := sysManager.WatchAllModels()
	c.Assert(err, jc.ErrorIsNil)
	defer w.Stop()

	deltasC := make(chan struct{}, 2)
	go func() {
		defer close(deltasC)
		for {
			_, err := w.Next()
			if err != nil {
				return
			}
			deltasC <- struct{}{}
		}
	}()
	// Read the first event.
	select {
	case <-deltasC:
	case <-time.After(testing.LongWait):
		c.Fatal("timed out")
	}
	// Wait a little while for the Next call to actually arrive.
	time.Sleep(testing.ShortWait)

	// We should be able to close the server instance
	// even when there's an outstanding Next call.
	srvStopped := make(chan struct{})
	go func() {
		srv.Stop()
		close(srvStopped)
	}()

	select {
	case <-srvStopped:
	case <-time.After(testing.LongWait):
		c.Fatal("timed out waiting for server to stop")
	}

	// Check that the Next call has returned too.
	select {
	case _, ok := <-deltasC:
		if ok {
			c.Fatalf("got unexpected event from deltasC")
		}
	case <-time.After(testing.LongWait):
		c.Fatal("timed out")
	}
}