github.com/nullne/docker@v1.13.0-rc1/api/server/router/plugin/plugin_routes.go (about)

     1  package plugin
     2  
     3  import (
     4  	"encoding/base64"
     5  	"encoding/json"
     6  	"net/http"
     7  	"strings"
     8  
     9  	"github.com/docker/docker/api/server/httputils"
    10  	"github.com/docker/docker/api/types"
    11  	"golang.org/x/net/context"
    12  )
    13  
    14  func (pr *pluginRouter) pullPlugin(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
    15  	if err := httputils.ParseForm(r); err != nil {
    16  		return err
    17  	}
    18  
    19  	metaHeaders := map[string][]string{}
    20  	for k, v := range r.Header {
    21  		if strings.HasPrefix(k, "X-Meta-") {
    22  			metaHeaders[k] = v
    23  		}
    24  	}
    25  
    26  	// Get X-Registry-Auth
    27  	authEncoded := r.Header.Get("X-Registry-Auth")
    28  	authConfig := &types.AuthConfig{}
    29  	if authEncoded != "" {
    30  		authJSON := base64.NewDecoder(base64.URLEncoding, strings.NewReader(authEncoded))
    31  		if err := json.NewDecoder(authJSON).Decode(authConfig); err != nil {
    32  			authConfig = &types.AuthConfig{}
    33  		}
    34  	}
    35  
    36  	privileges, err := pr.backend.Pull(r.FormValue("name"), metaHeaders, authConfig)
    37  	if err != nil {
    38  		return err
    39  	}
    40  	return httputils.WriteJSON(w, http.StatusOK, privileges)
    41  }
    42  
    43  func (pr *pluginRouter) createPlugin(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
    44  	if err := httputils.ParseForm(r); err != nil {
    45  		return err
    46  	}
    47  
    48  	options := &types.PluginCreateOptions{
    49  		RepoName: r.FormValue("name")}
    50  
    51  	if err := pr.backend.CreateFromContext(ctx, r.Body, options); err != nil {
    52  		return err
    53  	}
    54  	w.WriteHeader(http.StatusNoContent)
    55  	return nil
    56  }
    57  
    58  func (pr *pluginRouter) enablePlugin(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
    59  	return pr.backend.Enable(vars["name"])
    60  }
    61  
    62  func (pr *pluginRouter) disablePlugin(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
    63  	return pr.backend.Disable(vars["name"])
    64  }
    65  
    66  func (pr *pluginRouter) removePlugin(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
    67  	if err := httputils.ParseForm(r); err != nil {
    68  		return err
    69  	}
    70  
    71  	name := vars["name"]
    72  	config := &types.PluginRmConfig{
    73  		ForceRemove: httputils.BoolValue(r, "force"),
    74  	}
    75  	return pr.backend.Remove(name, config)
    76  }
    77  
    78  func (pr *pluginRouter) pushPlugin(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
    79  	if err := httputils.ParseForm(r); err != nil {
    80  		return err
    81  	}
    82  
    83  	metaHeaders := map[string][]string{}
    84  	for k, v := range r.Header {
    85  		if strings.HasPrefix(k, "X-Meta-") {
    86  			metaHeaders[k] = v
    87  		}
    88  	}
    89  
    90  	// Get X-Registry-Auth
    91  	authEncoded := r.Header.Get("X-Registry-Auth")
    92  	authConfig := &types.AuthConfig{}
    93  	if authEncoded != "" {
    94  		authJSON := base64.NewDecoder(base64.URLEncoding, strings.NewReader(authEncoded))
    95  		if err := json.NewDecoder(authJSON).Decode(authConfig); err != nil {
    96  			authConfig = &types.AuthConfig{}
    97  		}
    98  	}
    99  	return pr.backend.Push(vars["name"], metaHeaders, authConfig)
   100  }
   101  
   102  func (pr *pluginRouter) setPlugin(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
   103  	var args []string
   104  	if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
   105  		return err
   106  	}
   107  	if err := pr.backend.Set(vars["name"], args); err != nil {
   108  		return err
   109  	}
   110  	w.WriteHeader(http.StatusNoContent)
   111  	return nil
   112  }
   113  
   114  func (pr *pluginRouter) listPlugins(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
   115  	l, err := pr.backend.List()
   116  	if err != nil {
   117  		return err
   118  	}
   119  	return httputils.WriteJSON(w, http.StatusOK, l)
   120  }
   121  
   122  func (pr *pluginRouter) inspectPlugin(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
   123  	result, err := pr.backend.Inspect(vars["name"])
   124  	if err != nil {
   125  		return err
   126  	}
   127  	return httputils.WriteJSON(w, http.StatusOK, result)
   128  }