// ServiceSetCharm sets the charm for a given service. func (c *Client) ServiceSetCharm(args params.ServiceSetCharm) error { service, err := c.api.state.Service(args.ServiceName) if err != nil { return err } curl, err := charm.ParseURL(args.CharmUrl) if err != nil { return err } if curl.Schema != "cs" { return fmt.Errorf(`charm url has unsupported schema %q`, curl.Schema) } if curl.Revision < 0 { return fmt.Errorf("charm url must include revision") } conn, err := juju.NewConnFromState(c.api.state) if err != nil { return err } ch, err := conn.PutCharm(curl, CharmStore, false) if err != nil { return err } return service.SetCharm(ch, args.Force) }
// AddServiceUnits adds a given number of units to a service. func AddServiceUnits(state *state.State, args params.AddServiceUnits) ([]*state.Unit, error) { conn, err := juju.NewConnFromState(state) if err != nil { return nil, err } service, err := state.Service(args.ServiceName) if err != nil { return nil, err } if args.NumUnits < 1 { return nil, errors.New("must add at least one unit") } if args.NumUnits > 1 && args.ToMachineSpec != "" { return nil, errors.New("cannot use --num-units with --to") } return conn.AddUnits(service, args.NumUnits, args.ToMachineSpec) }
// serviceSetCharm sets the charm for the given service. func serviceSetCharm(state *state.State, service *state.Service, url string, force bool) error { curl, err := charm.ParseURL(url) if err != nil { return err } if curl.Schema != "cs" { return fmt.Errorf(`charm url has unsupported schema %q`, curl.Schema) } if curl.Revision < 0 { return fmt.Errorf("charm url must include revision") } conn, err := juju.NewConnFromState(state) if err != nil { return err } ch, err := conn.PutCharm(curl, CharmStore, false) if err != nil { return err } return service.SetCharm(ch, force) }
// ServiceDeploy fetches the charm from the charm store and deploys it. Local // charms are not supported. func (c *Client) ServiceDeploy(args params.ServiceDeploy) error { curl, err := charm.ParseURL(args.CharmUrl) if err != nil { return err } if curl.Schema != "cs" { return fmt.Errorf(`charm url has unsupported schema %q`, curl.Schema) } if curl.Revision < 0 { return fmt.Errorf("charm url must include revision") } conn, err := juju.NewConnFromState(c.api.state) if err != nil { return err } ch, err := conn.PutCharm(curl, CharmStore, false) if err != nil { return err } var settings charm.Settings if len(args.ConfigYAML) > 0 { settings, err = ch.Config().ParseSettingsYAML([]byte(args.ConfigYAML), args.ServiceName) } else if len(args.Config) > 0 { settings, err = ch.Config().ParseSettingsStrings(args.Config) } if err != nil { return err } _, err = conn.DeployService(juju.DeployServiceParams{ ServiceName: args.ServiceName, Charm: ch, NumUnits: args.NumUnits, ConfigSettings: settings, Constraints: args.Constraints, ToMachineSpec: args.ToMachineSpec, }) return err }
func (s *ConnSuite) TestNewConnFromState(c *C) { conn, err := juju.NewConnFromState(s.conn.State) c.Assert(err, IsNil) c.Assert(conn.Environ.Name(), Equals, "erewhemos") }