From f14c53980fa255d53da76ba64e8c04329359442a Mon Sep 17 00:00:00 2001 From: jackgr Date: Mon, 15 Feb 2016 11:52:43 -0800 Subject: [PATCH] Remove unneccessary download url retrieval. --- dm/dm.go | 28 ++++--------------------- expandybird/expander/expander.go | 24 ++++++++++++++++++++- expandybird/expander/expander_test.go | 30 ++++++++++++++++++++++++++- 3 files changed, 56 insertions(+), 26 deletions(-) diff --git a/dm/dm.go b/dm/dm.go index c1e25a5c0..ee7c2954b 100644 --- a/dm/dm.go +++ b/dm/dm.go @@ -245,15 +245,7 @@ func execute() { os.Exit(1) } - tUrls := getDownloadURLs(args[1]) - var tURL = "" - if len(tUrls) == 0 { - // Type is most likely a primitive. - tURL = args[1] - } else { - // TODO(vaikas): Support packages properly. - tURL = tUrls[0] - } + tURL := args[1] path := fmt.Sprintf("types/%s/instances", url.QueryEscape(tURL)) action := fmt.Sprintf("list deployed instances of type %s", tURL) callService(path, "GET", action, nil) @@ -431,24 +423,12 @@ func buildTemplateFromType(t string) *common.Template { } // Name the deployment after the type name. - name := t - - config := common.Configuration{Resources: []*common.Resource{&common.Resource{ - Name: name, - Type: getDownloadURLs(t)[0], - Properties: props, - }}} - - y, err := yaml.Marshal(config) + template, err := expander.NewTemplateFromType(t, t, props) if err != nil { - panic(fmt.Errorf("error: %s\ncannot create configuration for deployment: %v\n", err, config)) + panic(fmt.Errorf("cannot create configuration from type (%s): %s\n", t, err)) } - return &common.Template{ - Name: name, - Content: string(y), - // No imports, as this is a single type from repository. - } + return template } func marshalTemplate(template *common.Template) io.ReadCloser { diff --git a/expandybird/expander/expander.go b/expandybird/expander/expander.go index 758a71474..86ad3bf6b 100644 --- a/expandybird/expander/expander.go +++ b/expandybird/expander/expander.go @@ -6,7 +6,7 @@ you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 - + Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -46,6 +46,28 @@ func NewExpander(binary string) Expander { return &expander{binary} } +func NewTemplateFromType(name, typeName string, properties map[string]interface{}) (*common.Template, error) { + resource := &common.Resource{ + Name: name, + Type: typeName, + Properties: properties, + } + + config := common.Configuration{Resources: []*common.Resource{resource}} + content, err := yaml.Marshal(config) + if err != nil { + return nil, fmt.Errorf("error: %s\ncannot marshal configuration: %v\n", err, config) + } + + template := &common.Template{ + Name: name, + Content: string(content), + Imports: []*common.ImportFile{}, + } + + return template, nil +} + // NewTemplateFromArchive creates and returns a new template whose content // and imported files are read from the supplied archive. func NewTemplateFromArchive(name string, r io.Reader, importFileNames []string) (*common.Template, error) { diff --git a/expandybird/expander/expander_test.go b/expandybird/expander/expander_test.go index 64aef8aef..9c4677f78 100644 --- a/expandybird/expander/expander_test.go +++ b/expandybird/expander/expander_test.go @@ -6,7 +6,7 @@ you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 - + Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -28,6 +28,7 @@ import ( "strings" "testing" + "github.com/ghodss/yaml" "github.com/kubernetes/deployment-manager/common" ) @@ -107,6 +108,33 @@ func testExpandTemplateFromFile(t *testing.T, fileName, baseName string, importF expandAndVerifyOutput(t, actualOutput, description) } +var ( + testTemplateName = "expandybird" + testTemplateType = "replicatedservice.py" + testTemplateProperties = ` +service_port: 8080 +target_port: 8080 +container_port: 8080 +external_service: true +replicas: 3 +image: gcr.io/dm-k8s-testing/expandybird +labels: + app: expandybird +` +) + +func TestNewTemplateFromType(t *testing.T) { + var properties map[string]interface{} + if err := yaml.Unmarshal([]byte(testTemplateProperties), &properties); err != nil { + t.Fatalf("cannot unmarshal test data: %s", err) + } + + _, err := NewTemplateFromType(testTemplateName, testTemplateType, properties) + if err != nil { + t.Fatalf("cannot create template from type %s: %s", testTemplateType, err) + } +} + func TestNewTemplateFromReader(t *testing.T) { r := bytes.NewReader([]byte{}) if _, err := NewTemplateFromReader("test", r, nil); err == nil {