github.com/portworx/docker@v1.12.1/api/server/router/plugin/plugin_routes.go (about)

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