示例#1
0
// ResolveServiceNetworks takes a service spec and resolves network names to network IDs.
func ResolveServiceNetworks(ctx context.Context, c api.ControlClient, spec *api.ServiceSpec) error {
	if len(spec.Networks) == 0 {
		return nil
	}
	networks := make([]*api.ServiceSpec_NetworkAttachmentConfig, 0, len(spec.Networks))
	for _, na := range spec.Networks {
		n, err := GetNetwork(ctx, c, na.Target)
		if err != nil {
			return err
		}

		networks = append(networks, &api.ServiceSpec_NetworkAttachmentConfig{
			Target: n.ID,
		})
	}

	spec.Networks = networks
	return nil
}
示例#2
0
func parseNetworks(cmd *cobra.Command, spec *api.ServiceSpec, c api.ControlClient) error {
	flags := cmd.Flags()

	if !flags.Changed("network") {
		return nil
	}
	input, err := flags.GetString("network")
	if err != nil {
		return err
	}

	n, err := network.GetNetwork(common.Context(cmd), c, input)
	if err != nil {
		return err
	}

	spec.Networks = []*api.ServiceSpec_NetworkAttachmentConfig{
		{
			Target: n.ID,
		},
	}
	return nil
}