address CR comments

pull/141/head
vaikas-google 9 years ago
parent 711fc7b1bb
commit ad7c6b4cd8

@ -211,8 +211,7 @@ def ExpandTemplate(resource, imports, env, validate_schema=False):
# source_file could be a short version of the template (say github short name) # source_file could be a short version of the template (say github short name)
# so we need to potentially map this into the fully resolvable name. # so we need to potentially map this into the fully resolvable name.
if 'path' in imports[source_file]: if 'path' in imports[source_file] and imports[source_file]['path']:
if imports[source_file]['path']:
path = imports[source_file]['path'] path = imports[source_file]['path']
resource['imports'] = imports resource['imports'] = imports

@ -199,13 +199,13 @@ func putDeploymentHandlerFunc(w http.ResponseWriter, r *http.Request) {
func getPathVariable(w http.ResponseWriter, r *http.Request, variable, handler string) (string, error) { func getPathVariable(w http.ResponseWriter, r *http.Request, variable, handler string) (string, error) {
vars := mux.Vars(r) vars := mux.Vars(r)
retVariable, ok := vars[variable] ret, ok := vars[variable]
if !ok { if !ok {
e := fmt.Errorf("%s parameter not found in URL", variable) e := fmt.Errorf("%s parameter not found in URL", variable)
util.LogAndReturnError(handler, http.StatusBadRequest, e, w) util.LogAndReturnError(handler, http.StatusBadRequest, e, w)
return "", e return "", e
} }
return retVariable, nil return ret, nil
} }
func getTemplate(w http.ResponseWriter, r *http.Request, handler string) *common.Template { func getTemplate(w http.ResponseWriter, r *http.Request, handler string) *common.Template {

@ -31,6 +31,8 @@ const (
schemaSuffix = ".schema" schemaSuffix = ".schema"
) )
var re = regexp.MustCompile("github.com/(.*)/(.*)/(.*)/(.*):(.*)")
// TypeResolver finds Types in a Configuration which aren't yet reduceable to an import file // TypeResolver finds Types in a Configuration which aren't yet reduceable to an import file
// or primitive, and attempts to replace them with a template from a URL. // or primitive, and attempts to replace them with a template from a URL.
type TypeResolver interface { type TypeResolver interface {
@ -40,7 +42,6 @@ type TypeResolver interface {
type typeResolver struct { type typeResolver struct {
getter util.HTTPClient getter util.HTTPClient
maxUrls int maxUrls int
re *regexp.Regexp
rp registry.RegistryProvider rp registry.RegistryProvider
} }
@ -53,7 +54,6 @@ func NewTypeResolver() TypeResolver {
client.Timeout = timeout client.Timeout = timeout
ret.getter = util.NewHTTPClient(3, client, util.NewSleeper()) ret.getter = util.NewHTTPClient(3, client, util.NewSleeper())
ret.maxUrls = maxURLImports ret.maxUrls = maxURLImports
ret.re = regexp.MustCompile("github.com/(.*)/(.*)/(.*)/(.*):(.*)")
ret.rp = &registry.DefaultRegistryProvider{} ret.rp = &registry.DefaultRegistryProvider{}
return ret return ret
} }
@ -109,14 +109,14 @@ func (tr *typeResolver) ResolveTypes(config *common.Configuration, imports []*co
count := 0 count := 0
for len(toFetch) > 0 { for len(toFetch) > 0 {
//1. If short github URL, resolve to a download URL // 1. If short github URL, resolve to a download URL
//2. Fetch import URL. Exit if no URLs left // 2. Fetch import URL. Exit if no URLs left
//3. Check/handle HTTP status // 3. Check/handle HTTP status
//4. Store results in all ImportFiles from that URL // 4. Store results in all ImportFiles from that URL
//5. Check for the optional schema file at import URL + .schema // 5. Check for the optional schema file at import URL + .schema
//6. Repeat 2,3 for schema file // 6. Repeat 2,3 for schema file
//7. Add each schema import to fetch if not already done // 7. Add each schema import to fetch if not already done
//8. Mark URL done. Return to 1. // 8. Mark URL done. Return to 1.
if count >= tr.maxUrls { if count >= tr.maxUrls {
return nil, resolverError(config, return nil, resolverError(config,
fmt.Errorf("Number of imports exceeds maximum of %d", tr.maxUrls)) fmt.Errorf("Number of imports exceeds maximum of %d", tr.maxUrls))
@ -215,7 +215,7 @@ func (tr *typeResolver) MapFetchableURL(t string) (string, error) {
// for example: // for example:
// github.com/kubernetes/application-dm-templates/storage/redis:v1 // github.com/kubernetes/application-dm-templates/storage/redis:v1
func (tr *typeResolver) ShortTypeToDownloadURL(template string) (string, error) { func (tr *typeResolver) ShortTypeToDownloadURL(template string) (string, error) {
m := tr.re.FindStringSubmatch(template) m := re.FindStringSubmatch(template)
if len(m) != 6 { if len(m) != 6 {
return "", fmt.Errorf("Failed to parse short github url: %s", template) return "", fmt.Errorf("Failed to parse short github url: %s", template)
} }

@ -18,7 +18,6 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"reflect" "reflect"
"regexp"
"strings" "strings"
"testing" "testing"
@ -27,8 +26,6 @@ import (
"github.com/kubernetes/deployment-manager/common" "github.com/kubernetes/deployment-manager/common"
) )
var re = regexp.MustCompile("github.com/(.*)/(.*)/(.*)/(.*):(.*)")
type responseAndError struct { type responseAndError struct {
err error err error
code int code int
@ -96,7 +93,6 @@ func (tgr *testGithubRegistry) List() ([]registry.Type, error) {
func testUrlConversionDriver(c resolverTestCase, tests map[string]urlAndError, t *testing.T) { func testUrlConversionDriver(c resolverTestCase, tests map[string]urlAndError, t *testing.T) {
r := &typeResolver{ r := &typeResolver{
re: re,
rp: c.registryProvider, rp: c.registryProvider,
} }
for in, expected := range tests { for in, expected := range tests {
@ -115,7 +111,6 @@ func testDriver(c resolverTestCase, t *testing.T) {
r := &typeResolver{ r := &typeResolver{
getter: g, getter: g,
maxUrls: 5, maxUrls: 5,
re: re,
rp: c.registryProvider, rp: c.registryProvider,
} }

@ -48,6 +48,3 @@ type Registry interface {
// Get the download URL for a given template and version // Get the download URL for a given template and version
GetURL(t Type) (string, error) GetURL(t Type) (string, error)
} }

@ -14,17 +14,16 @@ limitations under the License.
package util package util
import ( import (
"strings" "regexp"
"log"
"github.com/kubernetes/deployment-manager/common" "github.com/kubernetes/deployment-manager/common"
) )
var re = regexp.MustCompile("github.com/(.*)/(.*)/(.*)/(.*):(.*)")
// IsTemplate returns whether a given type is a template. // IsTemplate returns whether a given type is a template.
func IsTemplate(t string, imports []*common.ImportFile) bool { func IsTemplate(t string, imports []*common.ImportFile) bool {
log.Printf("IsTemplate: %s : %+v", t, imports)
for _, imp := range imports { for _, imp := range imports {
log.Printf("Checking: %s", imp.Name)
if imp.Name == t { if imp.Name == t {
return true return true
} }
@ -38,16 +37,5 @@ func IsTemplate(t string, imports []*common.ImportFile) bool {
// for example: // for example:
// github.com/kubernetes/application-dm-templates/storage/redis:v1 // github.com/kubernetes/application-dm-templates/storage/redis:v1
func IsGithubShortType(t string) bool { func IsGithubShortType(t string) bool {
if !strings.HasPrefix(t, "github.com/") { return re.MatchString(t)
return false
}
s := strings.Split(t, "/")
if len(s) != 5 {
return false
}
v := strings.Split(s[4], ":")
if len(v) != 2 {
return false
}
return true
} }

Loading…
Cancel
Save