From 828521261d0e97cca24406ba8c64a54daa61defe Mon Sep 17 00:00:00 2001 From: mcardosos Date: Mon, 10 Oct 2016 17:57:45 -0700 Subject: [PATCH 01/14] Added rakefile (original in github.com/Azure/autorest) --- rakefile.rb | 292 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 292 insertions(+) create mode 100644 rakefile.rb diff --git a/rakefile.rb b/rakefile.rb new file mode 100644 index 000000000000..9dbc23d93d73 --- /dev/null +++ b/rakefile.rb @@ -0,0 +1,292 @@ +# This file is use to generate Azure SDK from swagger. +# Swaggers specs are located in git repo: https://github.com/Azure/azure-rest-api-specs +# AutoRest is located in git repo: https://github.com/Azure/autorest +# Build AutoRest before regenerating the SDK... +# cd local/AutoRest/git/repo +# gulp build +# Command to run rake file: +# rake SDK_VERSION=.. SWAGGER=path AUTOREST=path +# where SDK_VERSION is SDK version, +# SWAGGER is local swagger specs git repo (defaults to the github repo), +# AUTOREST is local AutoRest git repo + + +require 'fileutils' + +AUTOREST = "%s/autorest/src/core/AutoRest/bin/Debug/net451/win7-x64/AutoRest.exe" + +ARM_SWAGGERS = { + # asazure: {version: "2016-05-06"}, + authorization: {version: "2015-07-01"}, + batch: {version: "2015-12-01", swagger: "BatchManagement"}, + cdn: {version: "2016-04-02"}, + cognitiveservices: {version: "2016-02-01-preview"}, + compute: {version: "2016-03-30"}, #composite swagger (includes container service) + # containerservice: {version: "2016-03-30", swagger: "containerservice"}, + # commerce: {version: "2015-06-01-preview"}, + datalake_analytics: { + account: {version: "2015-10-01-preview"}, + # catalog: {version: "2016-06-01-preview"}, + # job: {version: "2016-03-20-preview"} + }, + datalake_store: { + account: {version: "2015-10-01-preview"}, + filesystem: {version: "2015-10-01-preview"} + }, + devtestlabs: {version: "2016-05-15", swagger: "DTL"}, + dns: {version: "2016-04-01"}, + eventhub: {version: "2015-08-01", swagger: "EventHub"}, + # graphrbac: {version: "1.6"}, # composite swagger + # insights (composite swagger) + intune: {version: "2015-01-14-preview"}, + iothub: {version: "2016-02-03"}, + keyvault: {version: "2015-06-01"}, + logic: {version: "2016-06-01"}, #composite swagger + # machine learning has two swaggers, but not a composite swagger + # this service should follow the same structure as the resources service and clients + machinelearning: {version: "2016-05-01-preview", swagger: "webservices"}, + # machinelearning: { + # webservices: {version: "2016-05-01-preview", swagger: "webservices"}, + # commitmentplans: {version: "2016-05-01-preview", swagger: "commitmentPlans"} + # }, + mediaservices: {version: "2015-10-01", swagger: "media"}, + mobileengagement: {version: "2014-12-01", swagger: "mobile-engagement"}, + network: {version: "2016-09-01"}, + notificationhubs: {version: "2016-03-01"}, + powerbiembedded: {version: "2016-01-29"}, + # recoveryservices: {version: "2016-06-01"}, + # recoveryservicesbackup: {version: "2016-06-01"}, + redis: {version: "2016-04-01"}, + resources: { + features: {version: "2015-12-01"}, + locks: {version: "2016-09-01"}, + policy: {version: "2016-04-01"}, + resources: {version: "2016-09-01"}, + # AutoRest Go generator has a bug and generates an ugly SDK for subscription newest API version + # https://github.com/Azure/autorest/issues/1477 + # subscriptions: {version: "2016-06-01"} + subscriptions: {version: "2015-11-01"} + }, + scheduler: {version: "2016-03-01"}, + search: {version: "2015-02-28"}, + servermanagement: {version: "2016-07-01-preview"}, + servicebus: {version: "2015-08-01"}, + sql: {version: "2015-05-01"}, + storage: {version: "2016-01-01"}, + trafficmanager: {version: "2015-11-01"}, + web: {version: "2015-08-01", swagger: "service"} # composite swagger +} + +DATAPLANE_SWAGGERS = { + # batch: {version: "2016-07-01.3.1", swagger: "BatchService"}, + # insights (composite swagger) + keyvault: {version: "2015-06-01"}, + # same thing as machine learning, two swaggers but no comp swagger + # search: { + # searchindex: {version: "2015-02-28"}, + # searchservice: {version: "2015-02-28"} + # }, + # servicefabric: {version: "2016-01-28"} +} + +MISPLACED_SWAGGERS = { + # datalake_analytics: { + # catalog: {version: "2016-06-01-preview"}, + # job: {version: "2016-03-20-preview"} + # }, + # datalake_store: { + # filesystem: {version: "2015-10-01-preview"} + # }, +} + +class Service + GO_NAMESPACE = "github.com/Azure/azure-sdk-for-go/%s/%s" + INPUT_PATH = "%s/azure-rest-api-specs/%s%s/swagger/%s.json" + OUTPUT_PATH = "#{ENV['GOPATH']}/src/%s" + + attr :name + attr :fullname + attr :namespace + attr :packages + attr :task_name + attr :version + attr :plane + + attr :spec_root + + attr :input_path + attr :output_path + + def initialize(service, input_prefix, plane) + @plane = plane + @packages = service[:packages] + @name = @packages.last + @fullname = @packages.map{|package| package.to_s.gsub(/_/,'-')}.join('/') + @namespace = sprintf(GO_NAMESPACE, @plane, @fullname) + @task_name = plane + ':' + @packages.join(':') + @version = service[:version] + if ENV['SWAGGER'] == nil + spec_root = "https://raw.githubusercontent.com/Azure/azure-rest-api-specs/master/%s%s/swagger/%s.json" + else + spec_root = sprintf("%s/azure-rest-api-specs/%s", ENV['SWAGGER'], "%s%s/swagger/%s.json") + end + swagger = service[:swagger] || @name + @input_path = sprintf(spec_root, input_prefix, [@fullname, @version].join('/'), swagger) + @output_path = sprintf(OUTPUT_PATH, @namespace) + end +end + +def to_services(m, h, ip, plane, *p) + if h.keys.include?(:version) + h[:packages] = p.reverse + m << Service.new(h, ip, plane) + else + h.keys.each do |k| + to_services(m, h[k], ip, plane, k, *p) + end + end + m +end + +services = to_services([], ARM_SWAGGERS, "arm-", "arm") +services = to_services(services, DATAPLANE_SWAGGERS, "", "dataplane") +services = to_services(services, MISPLACED_SWAGGERS, "arm-", "dataplane") + +desc "Generate, format, and build all services" +task :default => 'generate:all' + +desc "List the known services" +task :services do + services.each do |service| + puts "#{service.task_name}" + end +end + +namespace :generate do + desc "Generate all services" + task :all do + services.each {|service| Rake::Task["generate:#{service.task_name}"].execute } + end + + services.each do |service| + desc "Generate the #{service.task_name} service" + task service.task_name.to_sym do + generate(service) + end + end +end + +namespace :go do + namespace :delete do + desc "Delete all generated services" + task :all do + services.each {|service| delete(service) } + end + + services.each do |service| + desc "Delete the #{service.task_name} service" + task service.task_name.to_sym do + delete(service) + end + end + end + + namespace :format do + desc "Format all generated services" + task :all do + services.each {|service| format(service) } + end + + services.each do |service| + desc "Format the #{service.task_name} service" + task service.task_name.to_sym do + format(service) + end + end + end + + namespace :build do + desc "Build all generated services" + task :all do + services.each {|service| build(service) } + end + + services.each do |service| + desc "Build the #{service.task_name} service" + task service.task_name.to_sym do + build(service) + end + end + end + + namespace :lint do + desc "Lint all generated services" + task :all do + services.each {|service| lint(service) } + end + + services.each do |service| + desc "Lint the #{service.task_name} service" + task service.task_name.to_sym do + lint(service) + end + end + end + + namespace :vet do + desc "Vet all generated services" + task :all do + services.each {|service| vet(service) } + end + + services.each do |service| + desc "Vet the #{service.task_name} service" + task service.task_name.to_sym do + vet(service) + end + end + end +end + +def generate(service) + s = "Generating #{service.plane} #{service.fullname}" + puts "#{s} #{"=" * (80 - s.length)}" + delete(service) + s = `#{sprintf(AUTOREST, ENV['AUTOREST'])} -AddCredentials -CodeGenerator Go -Header MICROSOFT_APACHE -Input #{service.input_path} -Namespace #{service.namespace} -OutputDirectory #{service.output_path} -Modeler Swagger -pv #{ENV['SDK_VERSION']}` + raise "Failed generating #{service.plane} #{service.fullname}.#{service.inspect}" if s =~ /.*FATAL.*/ + puts s + + format(service) + build(service) + lint(service) + vet(service) +end + +def delete(service) + puts "Deleting #{service.plane} #{service.fullname}" + FileUtils.rmtree(service.output_path) +end + +def format(service) + puts "Formatting #{service.plane} #{service.fullname}" + s = `gofmt -w #{service.output_path}` + raise "Formatting #{service.output_path} failed:\n#{s}\n" if $?.exitstatus > 0 +end + +def build(service) + puts "Building #{service.plane} #{service.fullname}" + s = `go build #{service.namespace}` + raise "Building #{service.namespace} failed:\n#{s}\n" if $?.exitstatus > 0 +end + +def lint(service) + puts "Linting #{service.plane} #{service.fullname}" + s = `#{ENV["GOPATH"]}\\bin\\golint #{service.namespace}` + print s +end + +def vet(service) + puts "Vetting #{service.plane} #{service.fullname}" + s = `go vet #{service.namespace}` + print s +end From 6eb4fe0b6e2f293f6db888219ebbd2b3ce546c02 Mon Sep 17 00:00:00 2001 From: mcardosos Date: Tue, 18 Oct 2016 16:44:07 -0700 Subject: [PATCH 02/14] Rakefile translated to golang --- Gododir/gen.go | 480 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 480 insertions(+) create mode 100644 Gododir/gen.go diff --git a/Gododir/gen.go b/Gododir/gen.go new file mode 100644 index 000000000000..de01ceb9d7a0 --- /dev/null +++ b/Gododir/gen.go @@ -0,0 +1,480 @@ +package main + +// To run this package... +// go run gen.go -- --sdk 3.14.16 + +import ( + "bytes" + "fmt" + "os" + "os/exec" + "strings" + + do "gopkg.in/godo.v2" +) + +type Service struct { + Name string + Fullname string + Namespace string + Packages []string + TaskName string + Version string + Input string + Output string + Swagger string + SubServices []Service +} + +type Mapping struct { + Plane string + InputPrefix string + Services []Service +} + +var ( + gopath = os.Getenv("GOPATH") + sdkVersion string + autorestDir string + swaggersDir string + deps do.S + services = []*Service{} + mapping = []Mapping{ + { + Plane: "arm", + InputPrefix: "arm-", + Services: []Service{ + // { + // Name: "asazure", + // Version: "2016-05-06" + // }, + { + Name: "authorization", + Version: "2015-07-01", + }, + { + Name: "batch", + Version: "2015-12-01", + Swagger: "BatchManagement", + }, + { + Name: "cdn", + Version: "2016-10-02", + }, + { + Name: "cognitiveservices", + Version: "2016-02-01-preview", + }, + // { + // Name: "commerce", + // Version: "2015-06-01-preview", + // }, + { + Name: "compute", + Version: "2016-03-30", + }, + { + Name: "containerservice", + Version: "2016-03-30", + Swagger: "containerService", + Input: "compute", + }, + { + Name: "datalake-analytics", + SubServices: []Service{ + { + Name: "account", + Version: "2015-10-01-preview", + }, + }, + }, + { + Name: "datalake-store", + SubServices: []Service{ + { + Name: "account", + Version: "2015-10-01-preview", + }, + }, + }, + { + Name: "devtestlabs", + Version: "2016-05-15", + Swagger: "DTL", + }, + { + Name: "dns", + Version: "2016-04-01", + }, + { + Name: "eventhub", + Version: "2015-08-01", + Swagger: "EventHub", + }, + // { + // Name: "graphrbac", + // Version: "1.6", + // // Composite swagger + // }, + // { + // Name: "insights", + // // Composite swagger + // }, + { + Name: "intune", + Version: "2015-01-14-preview", + }, + { + Name: "iothub", + Version: "2016-02-03", + }, + { + Name: "keyvault", + Version: "2015-06-01", + }, + { + Name: "logic", + Version: "2016-06-01", + // composite swagger + }, + { + Name: "machinelearning", + SubServices: []Service{ + { + Name: "webservices", + Version: "2016-05-01-preview", + Input: "machinelearning", + }, + { + Name: "commitmentplans", + Version: "2016-05-01-preview", + Swagger: "commitmentPlans", + Input: "machinelearning", + }, + }, + }, + { + Name: "mediaservices", + Version: "2015-10-01", + Swagger: "media", + }, + { + Name: "mobileengagement", + Version: "2014-12-01", + Swagger: "mobile-engagement", + }, + { + Name: "network", + Version: "2016-09-01", + }, + { + Name: "notificationhubs", + Version: "2016-03-01", + }, + { + Name: "powerbiembedded", + Version: "2016-01-29", + }, + // { + // Name: "recoveryservices", + // Version: "2016-06-01", + // }, + // { + // Name: "recoveryservicesbackup", + // Version: "2016-06-01", + // }, + { + Name: "redis", + Version: "2016-04-01", + }, + { + Name: "resources", + SubServices: []Service{ + { + Name: "features", + Version: "2015-12-01", + }, + { + Name: "locks", + Version: "2016-09-01", + }, + { + Name: "policy", + Version: "2016-04-01", + }, + { + Name: "resources", + Version: "2016-09-01", + }, + { + Name: "subscriptions", + // AutoRest Go generator has a bug and generates an ugly SDK for subscription newest API version + // https://github.com/Azure/autorest/issues/1477 + // Version: "2016-06-01", + Version: "2015-11-01", + }, + }, + }, + { + Name: "scheduler", + Version: "2016-03-01", + }, + { + Name: "search", + Version: "2015-02-28", + }, + { + Name: "servermanagement", + Version: "2016-07-01-preview", + }, + { + Name: "sql", + Version: "2015-05-01", + }, + { + Name: "storage", + Version: "2016-01-01", + }, + { + Name: "trafficmanager", + Version: "2015-11-01", + }, + { + Name: "web", + Version: "2015-08-01", + Swagger: "service", + // enormous composite swagger + }, + }, + }, + { + // Plane: "dataplane", + // InputPrefix: "", + // Services: []Service{ + // { + // Name: "batch", + // Version: "2016-07-01.3.1", + // Swagger: "BatchService", + // }, + // { + // Name: "insights", + // // composite swagger + // }, + // { + // Name: "keyvault", + // Version: "2015-06-01", + // }, + // { + // Name: "search",' + // Version: "2015-02-28" + // // There are 2 files, but no composite swagger... + // }, + // { + // Name: "servicefabric", + // Version: "2016-01-28", + // }, + // }, + }, + { + Plane: "", + InputPrefix: "arm-", + Services: []Service{ + { + Name: "datalake-store", + SubServices: []Service{ + { + Name: "filesystem", + Version: "2015-10-01-preview", + }, + }, + }, + // { + // Name: "datalake-analytics", + // SubServices: []Service{ + // { + // Name: "catalog", + // Version: "2016-06-01-preview", + // }, + // { + // Name: "job", + // Version: "2016-03-20-preview", + // }, + // }, + // }, + }, + }, + } +) + +func main() { + for _, swaggerGroup := range mapping { + swg := swaggerGroup + for _, service := range swg.Services { + s := service + InitAndAddService(&s, swg.InputPrefix, swg.Plane) + } + } + do.Godo(tasks) +} + +func InitAndAddService(service *Service, inputPrefix, plane string) { + if service.Swagger == "" { + service.Swagger = service.Name + } + packages := append(service.Packages, service.Name) + service.TaskName = fmt.Sprintf("%s>%s", plane, strings.Join(packages, ">")) + service.Fullname = fmt.Sprintf("%s/%s", plane, strings.Join(packages, "/")) + if service.Input == "" { + service.Input = fmt.Sprintf("%s%s/%s/swagger/%s", inputPrefix, strings.Join(packages, "/"), service.Version, service.Swagger) + } else { + service.Input = fmt.Sprintf("%s%s/%s/swagger/%s", inputPrefix, service.Input, service.Version, service.Swagger) + } + service.Namespace = fmt.Sprintf("github.com/Azure/azure-sdk-for-go/%s", service.Fullname) + service.Output = fmt.Sprintf("%s/src/%s", gopath, service.Namespace) + + if service.SubServices != nil { + for _, subs := range service.SubServices { + ss := subs + ss.Packages = append(ss.Packages, service.Name) + InitAndAddService(&ss, inputPrefix, plane) + } + } else { + services = append(services, service) + deps = append(deps, service.TaskName) + } +} + +func tasks(p *do.Project) { + p.Task("default", do.S{"setvars", "generate:all"}, nil) + p.Task("setvars", nil, setVars) + p.Use("generate", generateTasks) + p.Use("gofmt", formatTasks) + p.Use("gobuild", buildTasks) + p.Use("golint", lintTasks) + p.Use("govet", vetTasks) + p.Use("delete", deleteTasks) +} + +func setVars(c *do.Context) { + if gopath == "" { + panic("Gopath not set\n") + } + + sdkVersion = c.Args.MustString("s", "sdk", "version") + autorestDir = c.Args.MayString("C:", "a", "ar", "autorest") + swaggersDir = c.Args.MayString("C:", "w", "sw", "swagger") +} + +func generateTasks(p *do.Project) { + addTasks(generate, p) +} + +func generate(service *Service) { + fmt.Printf("Generating %s...\n\n", service.Fullname) + delete(service) + + autorest := exec.Command(fmt.Sprintf("%s/autorest/src/core/AutoRest/bin/Debug/net451/win7-x64/autorest", autorestDir), + "-Input", fmt.Sprintf("%s/azure-rest-api-specs/%s.json", swaggersDir, service.Input), + "-CodeGenerator", "Go", + "-Header", "MICROSOFT_APACHE", + "-Namespace", service.Namespace, + "-OutputDirectory", service.Output, + "-Modeler", "Swagger", + "-pv", sdkVersion) + err := runner(autorest) + if err != nil { + panic(fmt.Errorf("Autorest error: %s\n", err)) + } + + format(service) + build(service) + lint(service) + vet(service) +} + +func deleteTasks(p *do.Project) { + addTasks(format, p) +} + +func delete(service *Service) { + fmt.Printf("Deleting %s...\n\n", service.Fullname) + err := os.RemoveAll(service.Output) + if err != nil { + panic(fmt.Sprintf("Error deleting %s : %s\n", service.Output, err)) + } +} + +func formatTasks(p *do.Project) { + addTasks(format, p) +} + +func format(service *Service) { + fmt.Printf("Formatting %s...\n\n", service.Fullname) + gofmt := exec.Command("gofmt", "-w", service.Output) + err := runner(gofmt) + if err != nil { + panic(fmt.Errorf("gofmt error: %s\n", err)) + } +} + +func buildTasks(p *do.Project) { + addTasks(build, p) +} + +func build(service *Service) { + fmt.Printf("Building %s...\n\n", service.Fullname) + gobuild := exec.Command("go", "build", service.Namespace) + err := runner(gobuild) + if err != nil { + panic(fmt.Errorf("go build error: %s\n", err)) + } +} + +func lintTasks(p *do.Project) { + addTasks(lint, p) +} + +func lint(service *Service) { + fmt.Printf("Linting %s...\n\n", service.Fullname) + golint := exec.Command(fmt.Sprintf("%s/bin/golint", gopath), service.Namespace) + err := runner(golint) + if err != nil { + panic(fmt.Errorf("golint error: %s\n", err)) + } +} + +func vetTasks(p *do.Project) { + addTasks(vet, p) +} + +func vet(service *Service) { + fmt.Printf("Vetting %s...\n\n", service.Fullname) + govet := exec.Command("go", "vet", service.Namespace) + err := runner(govet) + if err != nil { + panic(fmt.Errorf("go vet error: %s\n", err)) + } +} + +func addTasks(fn func(*Service), p *do.Project) { + for _, service := range services { + s := service + p.Task(s.TaskName, nil, func(c *do.Context) { + fn(s) + }) + } + p.Task("all", deps, nil) +} + +func runner(cmd *exec.Cmd) error { + var stdout, stderr bytes.Buffer + cmd.Stdout, cmd.Stderr = &stdout, &stderr + err := cmd.Run() + if stdout.Len() > 0 { + fmt.Println(stdout.String()) + } + if stderr.Len() > 0 { + fmt.Println(stderr.String()) + } + return err +} From 00a01457935d8c405f7389f336d4a7e34305f8b6 Mon Sep 17 00:00:00 2001 From: mcardosos Date: Wed, 19 Oct 2016 13:43:17 -0700 Subject: [PATCH 03/14] Updated travis file --- .travis.yml | 6 +++++- Gododir/gen.go | 58 +++++++++++++++++++++++++------------------------- 2 files changed, 34 insertions(+), 30 deletions(-) diff --git a/.travis.yml b/.travis.yml index 11f35a0ac7cb..cc71a3e990ee 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,15 +9,17 @@ install: - go get -u github.com/Masterminds/glide - go get -u golang.org/x/net/context - go get -u github.com/HewlettPackard/gas + - go get -u gopkg.in/godo.v2/cmd/godo - export GO15VENDOREXPERIMENT=1 - glide install script: - - gas -skip=management/examples/*.go -skip=*vendor* ./... | tee gas-scan.txt + - gas -skip=management/examples/*.go -skip=*vendor* -skip=Gododir/* ./... | tee gas-scan.txt - test -z "$(grep 'Severity:\s*HIGH' gas-scan.txt)" - test -z "$(gofmt -s -l $(find ./arm/* -type d -print) | tee /dev/stderr)" - test -z "$(gofmt -s -l -w management | tee /dev/stderr)" - test -z "$(gofmt -s -l -w storage | tee /dev/stderr)" + - test -z "$(gofmt -s -l -w Gododir | tee /dev/stderr)" - go build -v ./... - test -z "$(go vet $(find ./arm/* -type d -print | grep -v '^./arm/resources$') | tee /dev/stderr)" - test -z "$(golint ./arm/... | tee /dev/stderr)" @@ -27,3 +29,5 @@ script: - go test -v ./management/... - test -z "$(golint ./management/... | grep -v 'should have comment' | grep -v 'stutters' | tee /dev/stderr)" - go vet ./management/... + - test -z "$(golint ./Gododir/... | tee /dev/stderr)" + - go vet ./Gododir/... \ No newline at end of file diff --git a/Gododir/gen.go b/Gododir/gen.go index de01ceb9d7a0..c3d60fa1302f 100644 --- a/Gododir/gen.go +++ b/Gododir/gen.go @@ -13,7 +13,7 @@ import ( do "gopkg.in/godo.v2" ) -type Service struct { +type service struct { Name string Fullname string Namespace string @@ -23,27 +23,27 @@ type Service struct { Input string Output string Swagger string - SubServices []Service + SubServices []service } -type Mapping struct { +type mapping struct { Plane string InputPrefix string - Services []Service + Services []service } var ( - gopath = os.Getenv("GOPATH") - sdkVersion string - autorestDir string - swaggersDir string - deps do.S - services = []*Service{} - mapping = []Mapping{ + gopath = os.Getenv("GOPATH") + sdkVersion string + autorestDir string + swaggersDir string + deps do.S + services = []*service{} + servicesMapping = []mapping{ { Plane: "arm", InputPrefix: "arm-", - Services: []Service{ + Services: []service{ // { // Name: "asazure", // Version: "2016-05-06" @@ -81,7 +81,7 @@ var ( }, { Name: "datalake-analytics", - SubServices: []Service{ + SubServices: []service{ { Name: "account", Version: "2015-10-01-preview", @@ -90,7 +90,7 @@ var ( }, { Name: "datalake-store", - SubServices: []Service{ + SubServices: []service{ { Name: "account", Version: "2015-10-01-preview", @@ -139,7 +139,7 @@ var ( }, { Name: "machinelearning", - SubServices: []Service{ + SubServices: []service{ { Name: "webservices", Version: "2016-05-01-preview", @@ -189,7 +189,7 @@ var ( }, { Name: "resources", - SubServices: []Service{ + SubServices: []service{ { Name: "features", Version: "2015-12-01", @@ -278,10 +278,10 @@ var ( { Plane: "", InputPrefix: "arm-", - Services: []Service{ + Services: []service{ { Name: "datalake-store", - SubServices: []Service{ + SubServices: []service{ { Name: "filesystem", Version: "2015-10-01-preview", @@ -307,17 +307,17 @@ var ( ) func main() { - for _, swaggerGroup := range mapping { + for _, swaggerGroup := range servicesMapping { swg := swaggerGroup for _, service := range swg.Services { s := service - InitAndAddService(&s, swg.InputPrefix, swg.Plane) + initAndAddService(&s, swg.InputPrefix, swg.Plane) } } do.Godo(tasks) } -func InitAndAddService(service *Service, inputPrefix, plane string) { +func initAndAddService(service *service, inputPrefix, plane string) { if service.Swagger == "" { service.Swagger = service.Name } @@ -336,7 +336,7 @@ func InitAndAddService(service *Service, inputPrefix, plane string) { for _, subs := range service.SubServices { ss := subs ss.Packages = append(ss.Packages, service.Name) - InitAndAddService(&ss, inputPrefix, plane) + initAndAddService(&ss, inputPrefix, plane) } } else { services = append(services, service) @@ -369,7 +369,7 @@ func generateTasks(p *do.Project) { addTasks(generate, p) } -func generate(service *Service) { +func generate(service *service) { fmt.Printf("Generating %s...\n\n", service.Fullname) delete(service) @@ -396,7 +396,7 @@ func deleteTasks(p *do.Project) { addTasks(format, p) } -func delete(service *Service) { +func delete(service *service) { fmt.Printf("Deleting %s...\n\n", service.Fullname) err := os.RemoveAll(service.Output) if err != nil { @@ -408,7 +408,7 @@ func formatTasks(p *do.Project) { addTasks(format, p) } -func format(service *Service) { +func format(service *service) { fmt.Printf("Formatting %s...\n\n", service.Fullname) gofmt := exec.Command("gofmt", "-w", service.Output) err := runner(gofmt) @@ -421,7 +421,7 @@ func buildTasks(p *do.Project) { addTasks(build, p) } -func build(service *Service) { +func build(service *service) { fmt.Printf("Building %s...\n\n", service.Fullname) gobuild := exec.Command("go", "build", service.Namespace) err := runner(gobuild) @@ -434,7 +434,7 @@ func lintTasks(p *do.Project) { addTasks(lint, p) } -func lint(service *Service) { +func lint(service *service) { fmt.Printf("Linting %s...\n\n", service.Fullname) golint := exec.Command(fmt.Sprintf("%s/bin/golint", gopath), service.Namespace) err := runner(golint) @@ -447,7 +447,7 @@ func vetTasks(p *do.Project) { addTasks(vet, p) } -func vet(service *Service) { +func vet(service *service) { fmt.Printf("Vetting %s...\n\n", service.Fullname) govet := exec.Command("go", "vet", service.Namespace) err := runner(govet) @@ -456,7 +456,7 @@ func vet(service *Service) { } } -func addTasks(fn func(*Service), p *do.Project) { +func addTasks(fn func(*service), p *do.Project) { for _, service := range services { s := service p.Task(s.TaskName, nil, func(c *do.Context) { From 40fb1439612e2b56f438e7a159aa4ba68acbf81a Mon Sep 17 00:00:00 2001 From: mcardosos Date: Wed, 19 Oct 2016 15:14:11 -0700 Subject: [PATCH 04/14] Removed rakefile --- .travis.yml | 2 +- rakefile.rb | 292 ---------------------------------------------------- 2 files changed, 1 insertion(+), 293 deletions(-) delete mode 100644 rakefile.rb diff --git a/.travis.yml b/.travis.yml index cc71a3e990ee..5d4cb15037c0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -30,4 +30,4 @@ script: - test -z "$(golint ./management/... | grep -v 'should have comment' | grep -v 'stutters' | tee /dev/stderr)" - go vet ./management/... - test -z "$(golint ./Gododir/... | tee /dev/stderr)" - - go vet ./Gododir/... \ No newline at end of file + - go vet ./Gododir/... diff --git a/rakefile.rb b/rakefile.rb deleted file mode 100644 index 9dbc23d93d73..000000000000 --- a/rakefile.rb +++ /dev/null @@ -1,292 +0,0 @@ -# This file is use to generate Azure SDK from swagger. -# Swaggers specs are located in git repo: https://github.com/Azure/azure-rest-api-specs -# AutoRest is located in git repo: https://github.com/Azure/autorest -# Build AutoRest before regenerating the SDK... -# cd local/AutoRest/git/repo -# gulp build -# Command to run rake file: -# rake SDK_VERSION=.. SWAGGER=path AUTOREST=path -# where SDK_VERSION is SDK version, -# SWAGGER is local swagger specs git repo (defaults to the github repo), -# AUTOREST is local AutoRest git repo - - -require 'fileutils' - -AUTOREST = "%s/autorest/src/core/AutoRest/bin/Debug/net451/win7-x64/AutoRest.exe" - -ARM_SWAGGERS = { - # asazure: {version: "2016-05-06"}, - authorization: {version: "2015-07-01"}, - batch: {version: "2015-12-01", swagger: "BatchManagement"}, - cdn: {version: "2016-04-02"}, - cognitiveservices: {version: "2016-02-01-preview"}, - compute: {version: "2016-03-30"}, #composite swagger (includes container service) - # containerservice: {version: "2016-03-30", swagger: "containerservice"}, - # commerce: {version: "2015-06-01-preview"}, - datalake_analytics: { - account: {version: "2015-10-01-preview"}, - # catalog: {version: "2016-06-01-preview"}, - # job: {version: "2016-03-20-preview"} - }, - datalake_store: { - account: {version: "2015-10-01-preview"}, - filesystem: {version: "2015-10-01-preview"} - }, - devtestlabs: {version: "2016-05-15", swagger: "DTL"}, - dns: {version: "2016-04-01"}, - eventhub: {version: "2015-08-01", swagger: "EventHub"}, - # graphrbac: {version: "1.6"}, # composite swagger - # insights (composite swagger) - intune: {version: "2015-01-14-preview"}, - iothub: {version: "2016-02-03"}, - keyvault: {version: "2015-06-01"}, - logic: {version: "2016-06-01"}, #composite swagger - # machine learning has two swaggers, but not a composite swagger - # this service should follow the same structure as the resources service and clients - machinelearning: {version: "2016-05-01-preview", swagger: "webservices"}, - # machinelearning: { - # webservices: {version: "2016-05-01-preview", swagger: "webservices"}, - # commitmentplans: {version: "2016-05-01-preview", swagger: "commitmentPlans"} - # }, - mediaservices: {version: "2015-10-01", swagger: "media"}, - mobileengagement: {version: "2014-12-01", swagger: "mobile-engagement"}, - network: {version: "2016-09-01"}, - notificationhubs: {version: "2016-03-01"}, - powerbiembedded: {version: "2016-01-29"}, - # recoveryservices: {version: "2016-06-01"}, - # recoveryservicesbackup: {version: "2016-06-01"}, - redis: {version: "2016-04-01"}, - resources: { - features: {version: "2015-12-01"}, - locks: {version: "2016-09-01"}, - policy: {version: "2016-04-01"}, - resources: {version: "2016-09-01"}, - # AutoRest Go generator has a bug and generates an ugly SDK for subscription newest API version - # https://github.com/Azure/autorest/issues/1477 - # subscriptions: {version: "2016-06-01"} - subscriptions: {version: "2015-11-01"} - }, - scheduler: {version: "2016-03-01"}, - search: {version: "2015-02-28"}, - servermanagement: {version: "2016-07-01-preview"}, - servicebus: {version: "2015-08-01"}, - sql: {version: "2015-05-01"}, - storage: {version: "2016-01-01"}, - trafficmanager: {version: "2015-11-01"}, - web: {version: "2015-08-01", swagger: "service"} # composite swagger -} - -DATAPLANE_SWAGGERS = { - # batch: {version: "2016-07-01.3.1", swagger: "BatchService"}, - # insights (composite swagger) - keyvault: {version: "2015-06-01"}, - # same thing as machine learning, two swaggers but no comp swagger - # search: { - # searchindex: {version: "2015-02-28"}, - # searchservice: {version: "2015-02-28"} - # }, - # servicefabric: {version: "2016-01-28"} -} - -MISPLACED_SWAGGERS = { - # datalake_analytics: { - # catalog: {version: "2016-06-01-preview"}, - # job: {version: "2016-03-20-preview"} - # }, - # datalake_store: { - # filesystem: {version: "2015-10-01-preview"} - # }, -} - -class Service - GO_NAMESPACE = "github.com/Azure/azure-sdk-for-go/%s/%s" - INPUT_PATH = "%s/azure-rest-api-specs/%s%s/swagger/%s.json" - OUTPUT_PATH = "#{ENV['GOPATH']}/src/%s" - - attr :name - attr :fullname - attr :namespace - attr :packages - attr :task_name - attr :version - attr :plane - - attr :spec_root - - attr :input_path - attr :output_path - - def initialize(service, input_prefix, plane) - @plane = plane - @packages = service[:packages] - @name = @packages.last - @fullname = @packages.map{|package| package.to_s.gsub(/_/,'-')}.join('/') - @namespace = sprintf(GO_NAMESPACE, @plane, @fullname) - @task_name = plane + ':' + @packages.join(':') - @version = service[:version] - if ENV['SWAGGER'] == nil - spec_root = "https://raw.githubusercontent.com/Azure/azure-rest-api-specs/master/%s%s/swagger/%s.json" - else - spec_root = sprintf("%s/azure-rest-api-specs/%s", ENV['SWAGGER'], "%s%s/swagger/%s.json") - end - swagger = service[:swagger] || @name - @input_path = sprintf(spec_root, input_prefix, [@fullname, @version].join('/'), swagger) - @output_path = sprintf(OUTPUT_PATH, @namespace) - end -end - -def to_services(m, h, ip, plane, *p) - if h.keys.include?(:version) - h[:packages] = p.reverse - m << Service.new(h, ip, plane) - else - h.keys.each do |k| - to_services(m, h[k], ip, plane, k, *p) - end - end - m -end - -services = to_services([], ARM_SWAGGERS, "arm-", "arm") -services = to_services(services, DATAPLANE_SWAGGERS, "", "dataplane") -services = to_services(services, MISPLACED_SWAGGERS, "arm-", "dataplane") - -desc "Generate, format, and build all services" -task :default => 'generate:all' - -desc "List the known services" -task :services do - services.each do |service| - puts "#{service.task_name}" - end -end - -namespace :generate do - desc "Generate all services" - task :all do - services.each {|service| Rake::Task["generate:#{service.task_name}"].execute } - end - - services.each do |service| - desc "Generate the #{service.task_name} service" - task service.task_name.to_sym do - generate(service) - end - end -end - -namespace :go do - namespace :delete do - desc "Delete all generated services" - task :all do - services.each {|service| delete(service) } - end - - services.each do |service| - desc "Delete the #{service.task_name} service" - task service.task_name.to_sym do - delete(service) - end - end - end - - namespace :format do - desc "Format all generated services" - task :all do - services.each {|service| format(service) } - end - - services.each do |service| - desc "Format the #{service.task_name} service" - task service.task_name.to_sym do - format(service) - end - end - end - - namespace :build do - desc "Build all generated services" - task :all do - services.each {|service| build(service) } - end - - services.each do |service| - desc "Build the #{service.task_name} service" - task service.task_name.to_sym do - build(service) - end - end - end - - namespace :lint do - desc "Lint all generated services" - task :all do - services.each {|service| lint(service) } - end - - services.each do |service| - desc "Lint the #{service.task_name} service" - task service.task_name.to_sym do - lint(service) - end - end - end - - namespace :vet do - desc "Vet all generated services" - task :all do - services.each {|service| vet(service) } - end - - services.each do |service| - desc "Vet the #{service.task_name} service" - task service.task_name.to_sym do - vet(service) - end - end - end -end - -def generate(service) - s = "Generating #{service.plane} #{service.fullname}" - puts "#{s} #{"=" * (80 - s.length)}" - delete(service) - s = `#{sprintf(AUTOREST, ENV['AUTOREST'])} -AddCredentials -CodeGenerator Go -Header MICROSOFT_APACHE -Input #{service.input_path} -Namespace #{service.namespace} -OutputDirectory #{service.output_path} -Modeler Swagger -pv #{ENV['SDK_VERSION']}` - raise "Failed generating #{service.plane} #{service.fullname}.#{service.inspect}" if s =~ /.*FATAL.*/ - puts s - - format(service) - build(service) - lint(service) - vet(service) -end - -def delete(service) - puts "Deleting #{service.plane} #{service.fullname}" - FileUtils.rmtree(service.output_path) -end - -def format(service) - puts "Formatting #{service.plane} #{service.fullname}" - s = `gofmt -w #{service.output_path}` - raise "Formatting #{service.output_path} failed:\n#{s}\n" if $?.exitstatus > 0 -end - -def build(service) - puts "Building #{service.plane} #{service.fullname}" - s = `go build #{service.namespace}` - raise "Building #{service.namespace} failed:\n#{s}\n" if $?.exitstatus > 0 -end - -def lint(service) - puts "Linting #{service.plane} #{service.fullname}" - s = `#{ENV["GOPATH"]}\\bin\\golint #{service.namespace}` - print s -end - -def vet(service) - puts "Vetting #{service.plane} #{service.fullname}" - s = `go vet #{service.namespace}` - print s -end From f1778775c513972ece465e91b97764ab958ae2b6 Mon Sep 17 00:00:00 2001 From: mcardosos Date: Fri, 28 Oct 2016 19:28:01 -0700 Subject: [PATCH 05/14] Included errcheck --- .travis.yml | 8 ++ management/examples/example.go | 14 ++- management/util.go | 6 +- management/vmutils/extensions.go | 4 +- storage/blob.go | 188 +++++++++++++++++++------------ storage/client.go | 24 ++-- storage/client_test.go | 4 +- storage/file.go | 66 +++++++---- storage/queue.go | 68 ++++++----- storage/table.go | 28 +++-- storage/table_entities.go | 29 +++-- storage/util.go | 8 +- 12 files changed, 287 insertions(+), 160 deletions(-) diff --git a/.travis.yml b/.travis.yml index 6a134afebfa1..b8dc90e9d824 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,6 +10,7 @@ install: - go get -u golang.org/x/net/context - go get -u github.com/HewlettPackard/gas - go get -u gopkg.in/godo.v2/cmd/godo + - go get -u github.com/kisielk/errcheck - export GO15VENDOREXPERIMENT=1 - glide install @@ -31,3 +32,10 @@ script: - go vet ./management/... - test -z "$(golint ./Gododir/... | tee /dev/stderr)" - go vet ./Gododir/... + - errcheck -ignoretests ./arm/... + - errcheck -ignoretests ./datalake-store/... + - errcheck -ignoretests ./Gododir/... + - errcheck -ignoretests ./management/... + - errcheck -ignoretests ./storage/... + + \ No newline at end of file diff --git a/management/examples/example.go b/management/examples/example.go index 3b89a5699542..f5f2fc2d0da3 100644 --- a/management/examples/example.go +++ b/management/examples/example.go @@ -48,13 +48,19 @@ func main() { fmt.Println("Create virtual machine") role := vmutils.NewVMConfiguration(dnsName, vmSize) - vmutils.ConfigureDeploymentFromPlatformImage( + if err := vmutils.ConfigureDeploymentFromPlatformImage( &role, vmImage, fmt.Sprintf("http://%s.blob.core.windows.net/%s/%s.vhd", storageAccount, dnsName, dnsName), - "") - vmutils.ConfigureForLinux(&role, dnsName, userName, userPassword) - vmutils.ConfigureWithPublicSSH(&role) + ""); err != nil { + panic(err) + } + if err := vmutils.ConfigureForLinux(&role, dnsName, userName, userPassword); err != nil { + panic(err) + } + if err := vmutils.ConfigureWithPublicSSH(&role); err != nil { + panic(err) + } fmt.Println("Deploy") operationID, err := virtualmachine.NewClient(client). diff --git a/management/util.go b/management/util.go index 72601757e550..420e1374bd36 100644 --- a/management/util.go +++ b/management/util.go @@ -5,7 +5,9 @@ import ( "net/http" ) -func getResponseBody(response *http.Response) ([]byte, error) { - defer response.Body.Close() +func getResponseBody(response *http.Response) (out []byte, err error) { + defer func() { + err = response.Body.Close() + }() return ioutil.ReadAll(response.Body) } diff --git a/management/vmutils/extensions.go b/management/vmutils/extensions.go index 90a869275b77..ec4bc0c9387c 100644 --- a/management/vmutils/extensions.go +++ b/management/vmutils/extensions.go @@ -58,7 +58,9 @@ func AddAzureDockerVMExtensionConfiguration(role *vm.Role, dockerPort int, versi return fmt.Errorf(errParamNotSpecified, "role") } - ConfigureWithExternalPort(role, "docker", dockerPort, dockerPort, vm.InputEndpointProtocolTCP) + if err := ConfigureWithExternalPort(role, "docker", dockerPort, dockerPort, vm.InputEndpointProtocolTCP); err != nil { + panic(err) + } publicConfiguration, err := createDockerPublicConfig(dockerPort) if err != nil { diff --git a/storage/blob.go b/storage/blob.go index 317620363d09..d545c8e54f83 100644 --- a/storage/blob.go +++ b/storage/blob.go @@ -368,41 +368,46 @@ var ( // pagination token and other response details. // // See https://msdn.microsoft.com/en-us/library/azure/dd179352.aspx -func (b BlobStorageClient) ListContainers(params ListContainersParameters) (ContainerListResponse, error) { +func (b BlobStorageClient) ListContainers(params ListContainersParameters) (clr ContainerListResponse, err error) { q := mergeParams(params.getParameters(), url.Values{"comp": {"list"}}) uri := b.client.getEndpoint(blobServiceName, "", q) headers := b.client.getStandardHeaders() - var out ContainerListResponse resp, err := b.client.exec("GET", uri, headers, nil) if err != nil { - return out, err + return clr, err } - defer resp.body.Close() + defer func() { + err = resp.body.Close() + }() - err = xmlUnmarshal(resp.body, &out) - return out, err + err = xmlUnmarshal(resp.body, &clr) + return clr, err } // CreateContainer creates a blob container within the storage account // with given name and access level. Returns error if container already exists. // // See https://msdn.microsoft.com/en-us/library/azure/dd179468.aspx -func (b BlobStorageClient) CreateContainer(name string, access ContainerAccessType) error { +func (b BlobStorageClient) CreateContainer(name string, access ContainerAccessType) (err error) { resp, err := b.createContainer(name, access) if err != nil { return err } - defer resp.body.Close() + defer func() { + err = resp.body.Close() + }() return checkRespCode(resp.statusCode, []int{http.StatusCreated}) } // CreateContainerIfNotExists creates a blob container if it does not exist. Returns // true if container is newly created or false if container already exists. -func (b BlobStorageClient) CreateContainerIfNotExists(name string, access ContainerAccessType) (bool, error) { +func (b BlobStorageClient) CreateContainerIfNotExists(name string, access ContainerAccessType) (notExists bool, err error) { resp, err := b.createContainer(name, access) if resp != nil { - defer resp.body.Close() + defer func() { + err = resp.body.Close() + }() if resp.statusCode == http.StatusCreated || resp.statusCode == http.StatusConflict { return resp.statusCode == http.StatusCreated, nil } @@ -423,14 +428,16 @@ func (b BlobStorageClient) createContainer(name string, access ContainerAccessTy // ContainerExists returns true if a container with given name exists // on the storage account, otherwise returns false. -func (b BlobStorageClient) ContainerExists(name string) (bool, error) { +func (b BlobStorageClient) ContainerExists(name string) (exists bool, err error) { verb := "HEAD" uri := b.client.getEndpoint(blobServiceName, pathForContainer(name), url.Values{"restype": {"container"}}) headers := b.client.getStandardHeaders() resp, err := b.client.exec(verb, uri, headers, nil) if resp != nil { - defer resp.body.Close() + defer func() { + err = resp.body.Close() + }() if resp.statusCode == http.StatusOK || resp.statusCode == http.StatusNotFound { return resp.statusCode == http.StatusOK, nil } @@ -442,12 +449,14 @@ func (b BlobStorageClient) ContainerExists(name string) (bool, error) { // account. If the container does not exist returns error. // // See https://msdn.microsoft.com/en-us/library/azure/dd179408.aspx -func (b BlobStorageClient) DeleteContainer(name string) error { +func (b BlobStorageClient) DeleteContainer(name string) (err error) { resp, err := b.deleteContainer(name) if err != nil { return err } - defer resp.body.Close() + defer func() { + err = resp.body.Close() + }() return checkRespCode(resp.statusCode, []int{http.StatusAccepted}) } @@ -457,10 +466,12 @@ func (b BlobStorageClient) DeleteContainer(name string) error { // operation. // // See https://msdn.microsoft.com/en-us/library/azure/dd179408.aspx -func (b BlobStorageClient) DeleteContainerIfExists(name string) (bool, error) { +func (b BlobStorageClient) DeleteContainerIfExists(name string) (deleted bool, err error) { resp, err := b.deleteContainer(name) if resp != nil { - defer resp.body.Close() + defer func() { + err = resp.body.Close() + }() if resp.statusCode == http.StatusAccepted || resp.statusCode == http.StatusNotFound { return resp.statusCode == http.StatusAccepted, nil } @@ -480,33 +491,36 @@ func (b BlobStorageClient) deleteContainer(name string) (*storageResponse, error // pagination token and other information in the response of List Blobs call. // // See https://msdn.microsoft.com/en-us/library/azure/dd135734.aspx -func (b BlobStorageClient) ListBlobs(container string, params ListBlobsParameters) (BlobListResponse, error) { +func (b BlobStorageClient) ListBlobs(container string, params ListBlobsParameters) (blr BlobListResponse, err error) { q := mergeParams(params.getParameters(), url.Values{ "restype": {"container"}, "comp": {"list"}}) uri := b.client.getEndpoint(blobServiceName, pathForContainer(container), q) headers := b.client.getStandardHeaders() - var out BlobListResponse resp, err := b.client.exec("GET", uri, headers, nil) if err != nil { - return out, err + return blr, err } - defer resp.body.Close() + defer func() { + err = resp.body.Close() + }() - err = xmlUnmarshal(resp.body, &out) - return out, err + err = xmlUnmarshal(resp.body, &blr) + return blr, err } // BlobExists returns true if a blob with given name exists on the specified // container of the storage account. -func (b BlobStorageClient) BlobExists(container, name string) (bool, error) { +func (b BlobStorageClient) BlobExists(container, name string) (exists bool, err error) { verb := "HEAD" uri := b.client.getEndpoint(blobServiceName, pathForBlob(container, name), url.Values{}) headers := b.client.getStandardHeaders() resp, err := b.client.exec(verb, uri, headers, nil) if resp != nil { - defer resp.body.Close() + defer func() { + err = resp.body.Close() + }() if resp.statusCode == http.StatusOK || resp.statusCode == http.StatusNotFound { return resp.statusCode == http.StatusOK, nil } @@ -578,7 +592,7 @@ func (b BlobStorageClient) getBlobRange(container, name, bytesRange string, extr } // leasePut is common PUT code for the various aquire/release/break etc functions. -func (b BlobStorageClient) leaseCommonPut(container string, name string, headers map[string]string, expectedStatus int) (http.Header, error) { +func (b BlobStorageClient) leaseCommonPut(container string, name string, headers map[string]string, expectedStatus int) (h http.Header, err error) { params := url.Values{"comp": {"lease"}} uri := b.client.getEndpoint(blobServiceName, pathForBlob(container, name), params) @@ -586,7 +600,9 @@ func (b BlobStorageClient) leaseCommonPut(container string, name string, headers if err != nil { return nil, err } - defer resp.body.Close() + defer func() { + err = resp.body.Close() + }() if err := checkRespCode(resp.statusCode, []int{expectedStatus}); err != nil { return nil, err @@ -707,7 +723,7 @@ func (b BlobStorageClient) RenewLease(container string, name string, currentLeas // GetBlobProperties provides various information about the specified // blob. See https://msdn.microsoft.com/en-us/library/azure/dd179394.aspx -func (b BlobStorageClient) GetBlobProperties(container, name string) (*BlobProperties, error) { +func (b BlobStorageClient) GetBlobProperties(container, name string) (bp *BlobProperties, err error) { verb := "HEAD" uri := b.client.getEndpoint(blobServiceName, pathForBlob(container, name), url.Values{}) @@ -716,7 +732,9 @@ func (b BlobStorageClient) GetBlobProperties(container, name string) (*BlobPrope if err != nil { return nil, err } - defer resp.body.Close() + defer func() { + err = resp.body.Close() + }() if err := checkRespCode(resp.statusCode, []int{http.StatusOK}); err != nil { return nil, err @@ -769,7 +787,7 @@ func (b BlobStorageClient) GetBlobProperties(container, name string) (*BlobPrope // applications either. // // See https://msdn.microsoft.com/en-us/library/azure/ee691966.aspx -func (b BlobStorageClient) SetBlobProperties(container, name string, blobHeaders BlobHeaders) error { +func (b BlobStorageClient) SetBlobProperties(container, name string, blobHeaders BlobHeaders) (err error) { params := url.Values{"comp": {"properties"}} uri := b.client.getEndpoint(blobServiceName, pathForBlob(container, name), params) headers := b.client.getStandardHeaders() @@ -784,7 +802,9 @@ func (b BlobStorageClient) SetBlobProperties(container, name string, blobHeaders if err != nil { return err } - defer resp.body.Close() + defer func() { + err = resp.body.Close() + }() return checkRespCode(resp.statusCode, []int{http.StatusOK}) } @@ -797,7 +817,7 @@ func (b BlobStorageClient) SetBlobProperties(container, name string, blobHeaders // applications either. // // See https://msdn.microsoft.com/en-us/library/azure/dd179414.aspx -func (b BlobStorageClient) SetBlobMetadata(container, name string, metadata map[string]string, extraHeaders map[string]string) error { +func (b BlobStorageClient) SetBlobMetadata(container, name string, metadata map[string]string, extraHeaders map[string]string) (err error) { params := url.Values{"comp": {"metadata"}} uri := b.client.getEndpoint(blobServiceName, pathForBlob(container, name), params) headers := b.client.getStandardHeaders() @@ -813,7 +833,9 @@ func (b BlobStorageClient) SetBlobMetadata(container, name string, metadata map[ if err != nil { return err } - defer resp.body.Close() + defer func() { + err = resp.body.Close() + }() return checkRespCode(resp.statusCode, []int{http.StatusOK}) } @@ -824,7 +846,7 @@ func (b BlobStorageClient) SetBlobMetadata(container, name string, metadata map[ // names are case-insensitive.) // // See https://msdn.microsoft.com/en-us/library/azure/dd179414.aspx -func (b BlobStorageClient) GetBlobMetadata(container, name string) (map[string]string, error) { +func (b BlobStorageClient) GetBlobMetadata(container, name string) (metadata map[string]string, err error) { params := url.Values{"comp": {"metadata"}} uri := b.client.getEndpoint(blobServiceName, pathForBlob(container, name), params) headers := b.client.getStandardHeaders() @@ -833,13 +855,14 @@ func (b BlobStorageClient) GetBlobMetadata(container, name string) (map[string]s if err != nil { return nil, err } - defer resp.body.Close() + defer func() { + err = resp.body.Close() + }() if err := checkRespCode(resp.statusCode, []int{http.StatusOK}); err != nil { return nil, err } - metadata := make(map[string]string) for k, v := range resp.headers { // Can't trust CanonicalHeaderKey() to munge case // reliably. "_" is allowed in identifiers: @@ -877,7 +900,7 @@ func (b BlobStorageClient) CreateBlockBlob(container, name string) error { // PutBlock, and PutBlockList. // // See https://msdn.microsoft.com/en-us/library/azure/dd179451.aspx -func (b BlobStorageClient) CreateBlockBlobFromReader(container, name string, size uint64, blob io.Reader, extraHeaders map[string]string) error { +func (b BlobStorageClient) CreateBlockBlobFromReader(container, name string, size uint64, blob io.Reader, extraHeaders map[string]string) (err error) { path := fmt.Sprintf("%s/%s", container, name) uri := b.client.getEndpoint(blobServiceName, path, url.Values{}) headers := b.client.getStandardHeaders() @@ -892,7 +915,9 @@ func (b BlobStorageClient) CreateBlockBlobFromReader(container, name string, siz if err != nil { return err } - defer resp.body.Close() + defer func() { + err = resp.body.Close() + }() return checkRespCode(resp.statusCode, []int{http.StatusCreated}) } @@ -915,7 +940,7 @@ func (b BlobStorageClient) PutBlock(container, name, blockID string, chunk []byt // checked by the SDK). // // See https://msdn.microsoft.com/en-us/library/azure/dd135726.aspx -func (b BlobStorageClient) PutBlockWithLength(container, name, blockID string, size uint64, blob io.Reader, extraHeaders map[string]string) error { +func (b BlobStorageClient) PutBlockWithLength(container, name, blockID string, size uint64, blob io.Reader, extraHeaders map[string]string) (err error) { uri := b.client.getEndpoint(blobServiceName, pathForBlob(container, name), url.Values{"comp": {"block"}, "blockid": {blockID}}) headers := b.client.getStandardHeaders() headers["x-ms-blob-type"] = string(BlobTypeBlock) @@ -930,14 +955,16 @@ func (b BlobStorageClient) PutBlockWithLength(container, name, blockID string, s return err } - defer resp.body.Close() + defer func() { + err = resp.body.Close() + }() return checkRespCode(resp.statusCode, []int{http.StatusCreated}) } // PutBlockList saves list of blocks to the specified block blob. // // See https://msdn.microsoft.com/en-us/library/azure/dd179467.aspx -func (b BlobStorageClient) PutBlockList(container, name string, blocks []Block) error { +func (b BlobStorageClient) PutBlockList(container, name string, blocks []Block) (err error) { blockListXML := prepareBlockListRequest(blocks) uri := b.client.getEndpoint(blobServiceName, pathForBlob(container, name), url.Values{"comp": {"blocklist"}}) @@ -948,27 +975,30 @@ func (b BlobStorageClient) PutBlockList(container, name string, blocks []Block) if err != nil { return err } - defer resp.body.Close() + defer func() { + err = resp.body.Close() + }() return checkRespCode(resp.statusCode, []int{http.StatusCreated}) } // GetBlockList retrieves list of blocks in the specified block blob. // // See https://msdn.microsoft.com/en-us/library/azure/dd179400.aspx -func (b BlobStorageClient) GetBlockList(container, name string, blockType BlockListType) (BlockListResponse, error) { +func (b BlobStorageClient) GetBlockList(container, name string, blockType BlockListType) (blr BlockListResponse, err error) { params := url.Values{"comp": {"blocklist"}, "blocklisttype": {string(blockType)}} uri := b.client.getEndpoint(blobServiceName, pathForBlob(container, name), params) headers := b.client.getStandardHeaders() - var out BlockListResponse resp, err := b.client.exec("GET", uri, headers, nil) if err != nil { - return out, err + return blr, err } - defer resp.body.Close() + defer func() { + err = resp.body.Close() + }() - err = xmlUnmarshal(resp.body, &out) - return out, err + err = xmlUnmarshal(resp.body, &blr) + return blr, err } // PutPageBlob initializes an empty page blob with specified name and maximum @@ -976,7 +1006,7 @@ func (b BlobStorageClient) GetBlockList(container, name string, blockType BlockL // be created using this method before writing pages. // // See https://msdn.microsoft.com/en-us/library/azure/dd179451.aspx -func (b BlobStorageClient) PutPageBlob(container, name string, size int64, extraHeaders map[string]string) error { +func (b BlobStorageClient) PutPageBlob(container, name string, size int64, extraHeaders map[string]string) (err error) { path := fmt.Sprintf("%s/%s", container, name) uri := b.client.getEndpoint(blobServiceName, path, url.Values{}) headers := b.client.getStandardHeaders() @@ -991,7 +1021,9 @@ func (b BlobStorageClient) PutPageBlob(container, name string, size int64, extra if err != nil { return err } - defer resp.body.Close() + defer func() { + err = resp.body.Close() + }() return checkRespCode(resp.statusCode, []int{http.StatusCreated}) } @@ -1001,7 +1033,7 @@ func (b BlobStorageClient) PutPageBlob(container, name string, size int64, extra // with 512-byte boundaries and chunk must be of size multiplies by 512. // // See https://msdn.microsoft.com/en-us/library/ee691975.aspx -func (b BlobStorageClient) PutPage(container, name string, startByte, endByte int64, writeType PageWriteType, chunk []byte, extraHeaders map[string]string) error { +func (b BlobStorageClient) PutPage(container, name string, startByte, endByte int64, writeType PageWriteType, chunk []byte, extraHeaders map[string]string) (err error) { path := fmt.Sprintf("%s/%s", container, name) uri := b.client.getEndpoint(blobServiceName, path, url.Values{"comp": {"page"}}) headers := b.client.getStandardHeaders() @@ -1026,7 +1058,9 @@ func (b BlobStorageClient) PutPage(container, name string, startByte, endByte in if err != nil { return err } - defer resp.body.Close() + defer func() { + err = resp.body.Close() + }() return checkRespCode(resp.statusCode, []int{http.StatusCreated}) } @@ -1034,30 +1068,31 @@ func (b BlobStorageClient) PutPage(container, name string, startByte, endByte in // GetPageRanges returns the list of valid page ranges for a page blob. // // See https://msdn.microsoft.com/en-us/library/azure/ee691973.aspx -func (b BlobStorageClient) GetPageRanges(container, name string) (GetPageRangesResponse, error) { +func (b BlobStorageClient) GetPageRanges(container, name string) (pageRanges GetPageRangesResponse, err error) { path := fmt.Sprintf("%s/%s", container, name) uri := b.client.getEndpoint(blobServiceName, path, url.Values{"comp": {"pagelist"}}) headers := b.client.getStandardHeaders() - var out GetPageRangesResponse resp, err := b.client.exec("GET", uri, headers, nil) if err != nil { - return out, err + return pageRanges, err } - defer resp.body.Close() + defer func() { + err = resp.body.Close() + }() if err := checkRespCode(resp.statusCode, []int{http.StatusOK}); err != nil { - return out, err + return pageRanges, err } - err = xmlUnmarshal(resp.body, &out) - return out, err + err = xmlUnmarshal(resp.body, &pageRanges) + return pageRanges, err } // PutAppendBlob initializes an empty append blob with specified name. An // append blob must be created using this method before appending blocks. // // See https://msdn.microsoft.com/en-us/library/azure/dd179451.aspx -func (b BlobStorageClient) PutAppendBlob(container, name string, extraHeaders map[string]string) error { +func (b BlobStorageClient) PutAppendBlob(container, name string, extraHeaders map[string]string) (err error) { path := fmt.Sprintf("%s/%s", container, name) uri := b.client.getEndpoint(blobServiceName, path, url.Values{}) headers := b.client.getStandardHeaders() @@ -1071,7 +1106,9 @@ func (b BlobStorageClient) PutAppendBlob(container, name string, extraHeaders ma if err != nil { return err } - defer resp.body.Close() + defer func() { + err = resp.body.Close() + }() return checkRespCode(resp.statusCode, []int{http.StatusCreated}) } @@ -1079,7 +1116,7 @@ func (b BlobStorageClient) PutAppendBlob(container, name string, extraHeaders ma // AppendBlock appends a block to an append blob. // // See https://msdn.microsoft.com/en-us/library/azure/mt427365.aspx -func (b BlobStorageClient) AppendBlock(container, name string, chunk []byte, extraHeaders map[string]string) error { +func (b BlobStorageClient) AppendBlock(container, name string, chunk []byte, extraHeaders map[string]string) (err error) { path := fmt.Sprintf("%s/%s", container, name) uri := b.client.getEndpoint(blobServiceName, path, url.Values{"comp": {"appendblock"}}) headers := b.client.getStandardHeaders() @@ -1094,7 +1131,9 @@ func (b BlobStorageClient) AppendBlock(container, name string, chunk []byte, ext if err != nil { return err } - defer resp.body.Close() + defer func() { + err = resp.body.Close() + }() return checkRespCode(resp.statusCode, []int{http.StatusCreated}) } @@ -1114,7 +1153,7 @@ func (b BlobStorageClient) CopyBlob(container, name, sourceBlob string) error { return b.waitForBlobCopy(container, name, copyID) } -func (b BlobStorageClient) startBlobCopy(container, name, sourceBlob string) (string, error) { +func (b BlobStorageClient) startBlobCopy(container, name, sourceBlob string) (copyID string, err error) { uri := b.client.getEndpoint(blobServiceName, pathForBlob(container, name), url.Values{}) headers := b.client.getStandardHeaders() @@ -1124,13 +1163,15 @@ func (b BlobStorageClient) startBlobCopy(container, name, sourceBlob string) (st if err != nil { return "", err } - defer resp.body.Close() + defer func() { + err = resp.body.Close() + }() if err := checkRespCode(resp.statusCode, []int{http.StatusAccepted, http.StatusCreated}); err != nil { return "", err } - copyID := resp.headers.Get("x-ms-copy-id") + copyID = resp.headers.Get("x-ms-copy-id") if copyID == "" { return "", errors.New("Got empty copy id header") } @@ -1166,12 +1207,14 @@ func (b BlobStorageClient) waitForBlobCopy(container, name, copyID string) error // DeleteBlob deletes the given blob from the specified container. // If the blob does not exists at the time of the Delete Blob operation, it // returns error. See https://msdn.microsoft.com/en-us/library/azure/dd179413.aspx -func (b BlobStorageClient) DeleteBlob(container, name string, extraHeaders map[string]string) error { +func (b BlobStorageClient) DeleteBlob(container, name string, extraHeaders map[string]string) (err error) { resp, err := b.deleteBlob(container, name, extraHeaders) if err != nil { return err } - defer resp.body.Close() + defer func() { + err = resp.body.Close() + }() return checkRespCode(resp.statusCode, []int{http.StatusAccepted}) } @@ -1179,12 +1222,14 @@ func (b BlobStorageClient) DeleteBlob(container, name string, extraHeaders map[s // blob is deleted with this call, returns true. Otherwise returns false. // // See https://msdn.microsoft.com/en-us/library/azure/dd179413.aspx -func (b BlobStorageClient) DeleteBlobIfExists(container, name string, extraHeaders map[string]string) (bool, error) { +func (b BlobStorageClient) DeleteBlobIfExists(container, name string, extraHeaders map[string]string) (deleted bool, err error) { resp, err := b.deleteBlob(container, name, extraHeaders) if resp != nil && (resp.statusCode == http.StatusAccepted || resp.statusCode == http.StatusNotFound) { return resp.statusCode == http.StatusAccepted, nil } - defer resp.body.Close() + defer func() { + err = resp.body.Close() + }() return false, err } @@ -1246,7 +1291,10 @@ func (b BlobStorageClient) GetBlobSASURI(container, name string, expiry time.Tim return "", err } - sig := b.client.computeHmac256(stringToSign) + sig, err := b.client.computeHmac256(stringToSign) + if err != nil { + return "", err + } sasParams := url.Values{ "sv": {b.client.apiVersion}, "se": {signedExpiry}, diff --git a/storage/client.go b/storage/client.go index 2816e03ec6c9..670c22785103 100644 --- a/storage/client.go +++ b/storage/client.go @@ -233,9 +233,12 @@ func (c Client) GetFileService() FileServiceClient { return FileServiceClient{c} } -func (c Client) createAuthorizationHeader(canonicalizedString string) string { - signature := c.computeHmac256(canonicalizedString) - return fmt.Sprintf("%s %s:%s", "SharedKey", c.getCanonicalizedAccountName(), signature) +func (c Client) createAuthorizationHeader(canonicalizedString string) (string, error) { + signature, err := c.computeHmac256(canonicalizedString) + if err != nil { + return "", err + } + return fmt.Sprintf("%s %s:%s", "SharedKey", c.getCanonicalizedAccountName(), signature), nil } func (c Client) getAuthorizationHeader(verb, url string, headers map[string]string) (string, error) { @@ -245,7 +248,7 @@ func (c Client) getAuthorizationHeader(verb, url string, headers map[string]stri } canonicalizedString := c.buildCanonicalizedString(verb, headers, canonicalizedResource) - return c.createAuthorizationHeader(canonicalizedString), nil + return c.createAuthorizationHeader(canonicalizedString) } func (c Client) getStandardHeaders() map[string]string { @@ -501,7 +504,10 @@ func (c Client) createSharedKeyLite(url string, headers map[string]string) (stri } strToSign := headers["x-ms-date"] + "\n" + can - hmac := c.computeHmac256(strToSign) + hmac, err := c.computeHmac256(strToSign) + if err != nil { + return "", err + } return fmt.Sprintf("SharedKeyLite %s:%s", c.accountName, hmac), nil } @@ -515,9 +521,11 @@ func (c Client) execTable(verb, url string, headers map[string]string, body io.R return c.execInternalJSON(verb, url, headers, body) } -func readResponseBody(resp *http.Response) ([]byte, error) { - defer resp.Body.Close() - out, err := ioutil.ReadAll(resp.Body) +func readResponseBody(resp *http.Response) (out []byte, err error) { + defer func() { + err = resp.Body.Close() + }() + out, err = ioutil.ReadAll(resp.Body) if err == io.EOF { err = nil } diff --git a/storage/client_test.go b/storage/client_test.go index bcbb4a901821..af090e48c57a 100644 --- a/storage/client_test.go +++ b/storage/client_test.go @@ -210,5 +210,7 @@ func (s *StorageClientSuite) Test_createAuthorizationHeader(c *chk.C) { canonicalizedString := `foobarzoo` expected := `SharedKey foo:h5U0ATVX6SpbFX1H6GNuxIMeXXCILLoIvhflPtuQZ30=` - c.Assert(cli.createAuthorizationHeader(canonicalizedString), chk.Equals, expected) + ah, err := cli.createAuthorizationHeader(canonicalizedString) + c.Assert(err, chk.IsNil) + c.Assert(ah, chk.Equals, expected) } diff --git a/storage/file.go b/storage/file.go index 2397587c886d..c04a84762841 100644 --- a/storage/file.go +++ b/storage/file.go @@ -90,32 +90,35 @@ func pathForFileShare(name string) string { // pagination token and other response details. // // See https://msdn.microsoft.com/en-us/library/azure/dd179352.aspx -func (f FileServiceClient) ListShares(params ListSharesParameters) (ShareListResponse, error) { +func (f FileServiceClient) ListShares(params ListSharesParameters) (slr ShareListResponse, err error) { q := mergeParams(params.getParameters(), url.Values{"comp": {"list"}}) uri := f.client.getEndpoint(fileServiceName, "", q) headers := f.client.getStandardHeaders() - var out ShareListResponse resp, err := f.client.exec("GET", uri, headers, nil) if err != nil { - return out, err + return slr, err } - defer resp.body.Close() + defer func() { + err = resp.body.Close() + }() - err = xmlUnmarshal(resp.body, &out) - return out, err + err = xmlUnmarshal(resp.body, &slr) + return slr, err } // CreateShare operation creates a new share under the specified account. If the // share with the same name already exists, the operation fails. // // See https://msdn.microsoft.com/en-us/library/azure/dn167008.aspx -func (f FileServiceClient) CreateShare(name string) error { +func (f FileServiceClient) CreateShare(name string) (err error) { resp, err := f.createShare(name) if err != nil { return err } - defer resp.body.Close() + defer func() { + err = resp.body.Close() + }() return checkRespCode(resp.statusCode, []int{http.StatusCreated}) } @@ -127,7 +130,9 @@ func (f FileServiceClient) ShareExists(name string) (bool, error) { resp, err := f.client.exec("HEAD", uri, headers, nil) if resp != nil { - defer resp.body.Close() + defer func() { + err = resp.body.Close() + }() if resp.statusCode == http.StatusOK || resp.statusCode == http.StatusNotFound { return resp.statusCode == http.StatusOK, nil } @@ -148,10 +153,12 @@ func (f FileServiceClient) GetShareURL(name string) string { // container already exists. // // See https://msdn.microsoft.com/en-us/library/azure/dn167008.aspx -func (f FileServiceClient) CreateShareIfNotExists(name string) (bool, error) { +func (f FileServiceClient) CreateShareIfNotExists(name string) (created bool, err error) { resp, err := f.createShare(name) if resp != nil { - defer resp.body.Close() + defer func() { + err = resp.body.Close() + }() if resp.statusCode == http.StatusCreated || resp.statusCode == http.StatusConflict { return resp.statusCode == http.StatusCreated, nil } @@ -171,7 +178,7 @@ func (f FileServiceClient) createShare(name string) (*storageResponse, error) { // GetShareProperties provides various information about the specified // file. See https://msdn.microsoft.com/en-us/library/azure/dn689099.aspx -func (f FileServiceClient) GetShareProperties(name string) (*ShareProperties, error) { +func (f FileServiceClient) GetShareProperties(name string) (sp *ShareProperties, err error) { uri := f.client.getEndpoint(fileServiceName, pathForFileShare(name), url.Values{"restype": {"share"}}) headers := f.client.getStandardHeaders() @@ -179,7 +186,9 @@ func (f FileServiceClient) GetShareProperties(name string) (*ShareProperties, er if err != nil { return nil, err } - defer resp.body.Close() + defer func() { + err = resp.body.Close() + }() if err := checkRespCode(resp.statusCode, []int{http.StatusOK}); err != nil { return nil, err @@ -200,7 +209,7 @@ func (f FileServiceClient) GetShareProperties(name string) (*ShareProperties, er // applications either. // // See https://msdn.microsoft.com/en-us/library/azure/mt427368.aspx -func (f FileServiceClient) SetShareProperties(name string, shareHeaders ShareHeaders) error { +func (f FileServiceClient) SetShareProperties(name string, shareHeaders ShareHeaders) (err error) { params := url.Values{} params.Set("restype", "share") params.Set("comp", "properties") @@ -218,7 +227,9 @@ func (f FileServiceClient) SetShareProperties(name string, shareHeaders ShareHea if err != nil { return err } - defer resp.body.Close() + defer func() { + err = resp.body.Close() + }() return checkRespCode(resp.statusCode, []int{http.StatusOK}) } @@ -228,12 +239,14 @@ func (f FileServiceClient) SetShareProperties(name string, shareHeaders ShareHea // collection. // // See https://msdn.microsoft.com/en-us/library/azure/dn689090.aspx -func (f FileServiceClient) DeleteShare(name string) error { +func (f FileServiceClient) DeleteShare(name string) (err error) { resp, err := f.deleteShare(name) if err != nil { return err } - defer resp.body.Close() + defer func() { + err = resp.body.Close() + }() return checkRespCode(resp.statusCode, []int{http.StatusAccepted}) } @@ -243,10 +256,12 @@ func (f FileServiceClient) DeleteShare(name string) error { // false otherwise. // // See https://msdn.microsoft.com/en-us/library/azure/dn689090.aspx -func (f FileServiceClient) DeleteShareIfExists(name string) (bool, error) { +func (f FileServiceClient) DeleteShareIfExists(name string) (deleted bool, err error) { resp, err := f.deleteShare(name) if resp != nil { - defer resp.body.Close() + defer func() { + err = resp.body.Close() + }() if resp.statusCode == http.StatusAccepted || resp.statusCode == http.StatusNotFound { return resp.statusCode == http.StatusAccepted, nil } @@ -272,7 +287,7 @@ func (f FileServiceClient) deleteShare(name string) (*storageResponse, error) { // applications either. // // See https://msdn.microsoft.com/en-us/library/azure/dd179414.aspx -func (f FileServiceClient) SetShareMetadata(name string, metadata map[string]string, extraHeaders map[string]string) error { +func (f FileServiceClient) SetShareMetadata(name string, metadata map[string]string, extraHeaders map[string]string) (err error) { params := url.Values{} params.Set("restype", "share") params.Set("comp", "metadata") @@ -291,7 +306,9 @@ func (f FileServiceClient) SetShareMetadata(name string, metadata map[string]str if err != nil { return err } - defer resp.body.Close() + defer func() { + err = resp.body.Close() + }() return checkRespCode(resp.statusCode, []int{http.StatusOK}) } @@ -302,7 +319,7 @@ func (f FileServiceClient) SetShareMetadata(name string, metadata map[string]str // names are case-insensitive.) // // See https://msdn.microsoft.com/en-us/library/azure/dd179414.aspx -func (f FileServiceClient) GetShareMetadata(name string) (map[string]string, error) { +func (f FileServiceClient) GetShareMetadata(name string) (metadata map[string]string, err error) { params := url.Values{} params.Set("restype", "share") params.Set("comp", "metadata") @@ -314,13 +331,14 @@ func (f FileServiceClient) GetShareMetadata(name string) (map[string]string, err if err != nil { return nil, err } - defer resp.body.Close() + defer func() { + err = resp.body.Close() + }() if err := checkRespCode(resp.statusCode, []int{http.StatusOK}); err != nil { return nil, err } - metadata := make(map[string]string) for k, v := range resp.headers { // Can't trust CanonicalHeaderKey() to munge case // reliably. "_" is allowed in identifiers: diff --git a/storage/queue.go b/storage/queue.go index 3ecf4aca0d21..567882b858b9 100644 --- a/storage/queue.go +++ b/storage/queue.go @@ -131,7 +131,7 @@ type QueueMetadataResponse struct { // Metadata is associated with the queue as name-value pairs. // // See https://msdn.microsoft.com/en-us/library/azure/dd179348.aspx -func (c QueueServiceClient) SetMetadata(name string, metadata map[string]string) error { +func (c QueueServiceClient) SetMetadata(name string, metadata map[string]string) (err error) { uri := c.client.getEndpoint(queueServiceName, pathForQueue(name), url.Values{"comp": []string{"metadata"}}) headers := c.client.getStandardHeaders() for k, v := range metadata { @@ -142,7 +142,9 @@ func (c QueueServiceClient) SetMetadata(name string, metadata map[string]string) if err != nil { return err } - defer resp.body.Close() + defer func() { + err = resp.body.Close() + }() return checkRespCode(resp.statusCode, []int{http.StatusNoContent}) } @@ -156,7 +158,7 @@ func (c QueueServiceClient) SetMetadata(name string, metadata map[string]string) // Because the way Golang's http client (and http.Header in particular) // canonicalize header names, the returned metadata names would always // be all lower case. -func (c QueueServiceClient) GetMetadata(name string) (QueueMetadataResponse, error) { +func (c QueueServiceClient) GetMetadata(name string) (qmr QueueMetadataResponse, err error) { qm := QueueMetadataResponse{} qm.UserDefinedMetadata = make(map[string]string) uri := c.client.getEndpoint(queueServiceName, pathForQueue(name), url.Values{"comp": []string{"metadata"}}) @@ -165,7 +167,9 @@ func (c QueueServiceClient) GetMetadata(name string) (QueueMetadataResponse, err if err != nil { return qm, err } - defer resp.body.Close() + defer func() { + err = resp.body.Close() + }() for k, v := range resp.headers { if len(v) != 1 { @@ -191,27 +195,31 @@ func (c QueueServiceClient) GetMetadata(name string) (QueueMetadataResponse, err // CreateQueue operation creates a queue under the given account. // // See https://msdn.microsoft.com/en-us/library/azure/dd179342.aspx -func (c QueueServiceClient) CreateQueue(name string) error { +func (c QueueServiceClient) CreateQueue(name string) (err error) { uri := c.client.getEndpoint(queueServiceName, pathForQueue(name), url.Values{}) headers := c.client.getStandardHeaders() resp, err := c.client.exec("PUT", uri, headers, nil) if err != nil { return err } - defer resp.body.Close() + defer func() { + err = resp.body.Close() + }() return checkRespCode(resp.statusCode, []int{http.StatusCreated}) } // DeleteQueue operation permanently deletes the specified queue. // // See https://msdn.microsoft.com/en-us/library/azure/dd179436.aspx -func (c QueueServiceClient) DeleteQueue(name string) error { +func (c QueueServiceClient) DeleteQueue(name string) (err error) { uri := c.client.getEndpoint(queueServiceName, pathForQueue(name), url.Values{}) resp, err := c.client.exec("DELETE", uri, c.client.getStandardHeaders(), nil) if err != nil { return err } - defer resp.body.Close() + defer func() { + err = resp.body.Close() + }() return checkRespCode(resp.statusCode, []int{http.StatusNoContent}) } @@ -229,7 +237,7 @@ func (c QueueServiceClient) QueueExists(name string) (bool, error) { // PutMessage operation adds a new message to the back of the message queue. // // See https://msdn.microsoft.com/en-us/library/azure/dd179346.aspx -func (c QueueServiceClient) PutMessage(queue string, message string, params PutMessageParameters) error { +func (c QueueServiceClient) PutMessage(queue string, message string, params PutMessageParameters) (err error) { uri := c.client.getEndpoint(queueServiceName, pathForQueueMessages(queue), params.getParameters()) req := putMessageRequest{MessageText: message} body, nn, err := xmlMarshal(req) @@ -242,20 +250,24 @@ func (c QueueServiceClient) PutMessage(queue string, message string, params PutM if err != nil { return err } - defer resp.body.Close() + defer func() { + err = resp.body.Close() + }() return checkRespCode(resp.statusCode, []int{http.StatusCreated}) } // ClearMessages operation deletes all messages from the specified queue. // // See https://msdn.microsoft.com/en-us/library/azure/dd179454.aspx -func (c QueueServiceClient) ClearMessages(queue string) error { +func (c QueueServiceClient) ClearMessages(queue string) (err error) { uri := c.client.getEndpoint(queueServiceName, pathForQueueMessages(queue), url.Values{}) resp, err := c.client.exec("DELETE", uri, c.client.getStandardHeaders(), nil) if err != nil { return err } - defer resp.body.Close() + defer func() { + err = resp.body.Close() + }() return checkRespCode(resp.statusCode, []int{http.StatusNoContent}) } @@ -263,44 +275,48 @@ func (c QueueServiceClient) ClearMessages(queue string) error { // queue. // // See https://msdn.microsoft.com/en-us/library/azure/dd179474.aspx -func (c QueueServiceClient) GetMessages(queue string, params GetMessagesParameters) (GetMessagesResponse, error) { - var r GetMessagesResponse +func (c QueueServiceClient) GetMessages(queue string, params GetMessagesParameters) (gmr GetMessagesResponse, err error) { uri := c.client.getEndpoint(queueServiceName, pathForQueueMessages(queue), params.getParameters()) resp, err := c.client.exec("GET", uri, c.client.getStandardHeaders(), nil) if err != nil { - return r, err + return gmr, err } - defer resp.body.Close() - err = xmlUnmarshal(resp.body, &r) - return r, err + defer func() { + err = resp.body.Close() + }() + err = xmlUnmarshal(resp.body, &gmr) + return gmr, err } // PeekMessages retrieves one or more messages from the front of the queue, but // does not alter the visibility of the message. // // See https://msdn.microsoft.com/en-us/library/azure/dd179472.aspx -func (c QueueServiceClient) PeekMessages(queue string, params PeekMessagesParameters) (PeekMessagesResponse, error) { - var r PeekMessagesResponse +func (c QueueServiceClient) PeekMessages(queue string, params PeekMessagesParameters) (pmr PeekMessagesResponse, err error) { uri := c.client.getEndpoint(queueServiceName, pathForQueueMessages(queue), params.getParameters()) resp, err := c.client.exec("GET", uri, c.client.getStandardHeaders(), nil) if err != nil { - return r, err + return pmr, err } - defer resp.body.Close() - err = xmlUnmarshal(resp.body, &r) - return r, err + defer func() { + err = resp.body.Close() + }() + err = xmlUnmarshal(resp.body, &pmr) + return pmr, err } // DeleteMessage operation deletes the specified message. // // See https://msdn.microsoft.com/en-us/library/azure/dd179347.aspx -func (c QueueServiceClient) DeleteMessage(queue, messageID, popReceipt string) error { +func (c QueueServiceClient) DeleteMessage(queue, messageID, popReceipt string) (err error) { uri := c.client.getEndpoint(queueServiceName, pathForMessage(queue, messageID), url.Values{ "popreceipt": {popReceipt}}) resp, err := c.client.exec("DELETE", uri, c.client.getStandardHeaders(), nil) if err != nil { return err } - defer resp.body.Close() + defer func() { + err = resp.body.Close() + }() return checkRespCode(resp.statusCode, []int{http.StatusNoContent}) } diff --git a/storage/table.go b/storage/table.go index 39e997503552..88be0f517764 100644 --- a/storage/table.go +++ b/storage/table.go @@ -39,7 +39,7 @@ func (c *TableServiceClient) getStandardHeaders() map[string]string { // QueryTables returns the tables created in the // *TableServiceClient storage account. -func (c *TableServiceClient) QueryTables() ([]AzureTable, error) { +func (c *TableServiceClient) QueryTables() (at []AzureTable, err error) { uri := c.client.getEndpoint(tableServiceName, tablesURIPath, url.Values{}) headers := c.getStandardHeaders() @@ -49,32 +49,36 @@ func (c *TableServiceClient) QueryTables() ([]AzureTable, error) { if err != nil { return nil, err } - defer resp.body.Close() + defer func() { + err = resp.body.Close() + }() if err := checkRespCode(resp.statusCode, []int{http.StatusOK}); err != nil { return nil, err } buf := new(bytes.Buffer) - buf.ReadFrom(resp.body) + if _, err := buf.ReadFrom(resp.body); err != nil { + return nil, err + } var respArray queryTablesResponse if err := json.Unmarshal(buf.Bytes(), &respArray); err != nil { return nil, err } - s := make([]AzureTable, len(respArray.TableName)) + at = make([]AzureTable, len(respArray.TableName)) for i, elem := range respArray.TableName { - s[i] = AzureTable(elem.TableName) + at[i] = AzureTable(elem.TableName) } - return s, nil + return at, nil } // CreateTable creates the table given the specific // name. This function fails if the name is not compliant // with the specification or the tables already exists. -func (c *TableServiceClient) CreateTable(table AzureTable) error { +func (c *TableServiceClient) CreateTable(table AzureTable) (err error) { uri := c.client.getEndpoint(tableServiceName, tablesURIPath, url.Values{}) headers := c.getStandardHeaders() @@ -93,7 +97,9 @@ func (c *TableServiceClient) CreateTable(table AzureTable) error { if err != nil { return err } - defer resp.body.Close() + defer func() { + err = resp.body.Close() + }() if err := checkRespCode(resp.statusCode, []int{http.StatusCreated}); err != nil { return err @@ -106,7 +112,7 @@ func (c *TableServiceClient) CreateTable(table AzureTable) error { // name. This function fails if the table is not present. // Be advised: DeleteTable deletes all the entries // that may be present. -func (c *TableServiceClient) DeleteTable(table AzureTable) error { +func (c *TableServiceClient) DeleteTable(table AzureTable) (err error) { uri := c.client.getEndpoint(tableServiceName, tablesURIPath, url.Values{}) uri += fmt.Sprintf("('%s')", string(table)) @@ -119,7 +125,9 @@ func (c *TableServiceClient) DeleteTable(table AzureTable) error { if err != nil { return err } - defer resp.body.Close() + defer func() { + err = resp.body.Close() + }() if err := checkRespCode(resp.statusCode, []int{http.StatusNoContent}); err != nil { return err diff --git a/storage/table_entities.go b/storage/table_entities.go index 1b5919cd130f..fd201a65517e 100644 --- a/storage/table_entities.go +++ b/storage/table_entities.go @@ -77,7 +77,7 @@ type getTableEntriesResponse struct { // // Example: // entities, cToken, err = tSvc.QueryTableEntities("table", cToken, reflect.TypeOf(entity), 20, "") -func (c *TableServiceClient) QueryTableEntities(tableName AzureTable, previousContToken *ContinuationToken, retType reflect.Type, top int, query string) ([]TableEntity, *ContinuationToken, error) { +func (c *TableServiceClient) QueryTableEntities(tableName AzureTable, previousContToken *ContinuationToken, retType reflect.Type, top int, query string) (te []TableEntity, ct *ContinuationToken, err error) { if top > maxTopParameter { return nil, nil, fmt.Errorf("top accepts at maximum %d elements. Requested %d instead", maxTopParameter, top) } @@ -107,7 +107,9 @@ func (c *TableServiceClient) QueryTableEntities(tableName AzureTable, previousCo if err != nil { return nil, contToken, err } - defer resp.body.Close() + defer func() { + err = resp.body.Close() + }() if err := checkRespCode(resp.statusCode, []int{http.StatusOK}); err != nil { return nil, contToken, err @@ -134,7 +136,7 @@ func (c *TableServiceClient) InsertEntity(table AzureTable, entity TableEntity) return err } -func (c *TableServiceClient) execTable(table AzureTable, entity TableEntity, specifyKeysInURL bool, method string) (int, error) { +func (c *TableServiceClient) execTable(table AzureTable, entity TableEntity, specifyKeysInURL bool, method string) (statusCode int, err error) { uri := c.client.getEndpoint(tableServiceName, pathForTable(table), url.Values{}) if specifyKeysInURL { uri += fmt.Sprintf("(PartitionKey='%s',RowKey='%s')", url.QueryEscape(entity.PartitionKey()), url.QueryEscape(entity.RowKey())) @@ -150,16 +152,15 @@ func (c *TableServiceClient) execTable(table AzureTable, entity TableEntity, spe headers["Content-Length"] = fmt.Sprintf("%d", buf.Len()) - var err error - var resp *odataResponse - - resp, err = c.client.execTable(method, uri, headers, &buf) + resp, err := c.client.execTable(method, uri, headers, &buf) if err != nil { return 0, err } - defer resp.body.Close() + defer func() { + err = resp.body.Close() + }() return resp.statusCode, nil } @@ -217,7 +218,9 @@ func (c *TableServiceClient) DeleteEntity(table AzureTable, entity TableEntity, if err != nil { return err } - defer resp.body.Close() + defer func() { + err = resp.body.Close() + }() if err := checkRespCode(resp.statusCode, []int{http.StatusNoContent}); err != nil { return err @@ -338,8 +341,12 @@ func deserializeEntity(retType reflect.Type, reader io.Reader) ([]TableEntity, e } // Reset PartitionKey and RowKey - tEntries[i].SetPartitionKey(pKey) - tEntries[i].SetRowKey(rKey) + if err := tEntries[i].SetPartitionKey(pKey); err != nil { + return nil, err + } + if err := tEntries[i].SetRowKey(rKey); err != nil { + return nil, err + } } return tEntries, nil diff --git a/storage/util.go b/storage/util.go index d71c6ce55d8e..a69efd012f07 100644 --- a/storage/util.go +++ b/storage/util.go @@ -15,10 +15,12 @@ import ( "time" ) -func (c Client) computeHmac256(message string) string { +func (c Client) computeHmac256(message string) (string, error) { h := hmac.New(sha256.New, c.accountKey) - h.Write([]byte(message)) - return base64.StdEncoding.EncodeToString(h.Sum(nil)) + if _, err := h.Write([]byte(message)); err != nil { + return "", fmt.Errorf("Failed computing to hmac256 : %v", err) + } + return base64.StdEncoding.EncodeToString(h.Sum(nil)), nil } func currentTimeRfc1123Formatted() string { From a7de7adc8f0b681072b71d9756bdc94138ae5589 Mon Sep 17 00:00:00 2001 From: mcardosos Date: Mon, 31 Oct 2016 10:59:29 -0700 Subject: [PATCH 06/14] Fix metadata --- storage/blob.go | 1 + storage/file.go | 1 + 2 files changed, 2 insertions(+) diff --git a/storage/blob.go b/storage/blob.go index d545c8e54f83..663871251ebc 100644 --- a/storage/blob.go +++ b/storage/blob.go @@ -863,6 +863,7 @@ func (b BlobStorageClient) GetBlobMetadata(container, name string) (metadata map return nil, err } + metadata = make(map[string]string) for k, v := range resp.headers { // Can't trust CanonicalHeaderKey() to munge case // reliably. "_" is allowed in identifiers: diff --git a/storage/file.go b/storage/file.go index c04a84762841..bbc0230ba63d 100644 --- a/storage/file.go +++ b/storage/file.go @@ -339,6 +339,7 @@ func (f FileServiceClient) GetShareMetadata(name string) (metadata map[string]st return nil, err } + metadata = make(map[string]string) for k, v := range resp.headers { // Can't trust CanonicalHeaderKey() to munge case // reliably. "_" is allowed in identifiers: From 1c9424944a28bb000678f088d766547df1b2882f Mon Sep 17 00:00:00 2001 From: mcardosos Date: Tue, 1 Nov 2016 15:04:04 -0700 Subject: [PATCH 07/14] Removed panic --- management/vmutils/extensions.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/management/vmutils/extensions.go b/management/vmutils/extensions.go index ec4bc0c9387c..751fd6a6cdbd 100644 --- a/management/vmutils/extensions.go +++ b/management/vmutils/extensions.go @@ -59,7 +59,7 @@ func AddAzureDockerVMExtensionConfiguration(role *vm.Role, dockerPort int, versi } if err := ConfigureWithExternalPort(role, "docker", dockerPort, dockerPort, vm.InputEndpointProtocolTCP); err != nil { - panic(err) + return err } publicConfiguration, err := createDockerPublicConfig(dockerPort) From 9ca7400f1105c5849a59adf9d2621949b164554b Mon Sep 17 00:00:00 2001 From: mcardosos Date: Thu, 8 Dec 2016 15:16:35 -0800 Subject: [PATCH 08/14] Ignoring resp.body.Close() errors --- .travis.yml | 6 +- management/util.go | 6 +- storage/blob.go | 184 ++++++++++++++------------------------ storage/client.go | 8 +- storage/file.go | 67 +++++--------- storage/queue.go | 68 ++++++-------- storage/table.go | 24 ++--- storage/table_entities.go | 16 ++-- 8 files changed, 139 insertions(+), 240 deletions(-) diff --git a/.travis.yml b/.travis.yml index b8dc90e9d824..7512a9f4ea82 100644 --- a/.travis.yml +++ b/.travis.yml @@ -32,10 +32,6 @@ script: - go vet ./management/... - test -z "$(golint ./Gododir/... | tee /dev/stderr)" - go vet ./Gododir/... - - errcheck -ignoretests ./arm/... - - errcheck -ignoretests ./datalake-store/... - - errcheck -ignoretests ./Gododir/... - - errcheck -ignoretests ./management/... - - errcheck -ignoretests ./storage/... + - test -z "$(set -e; for v in ./arm... ./datalake-store/... ./Gododir/...; do (errcheck -ignoretests "$v" | grep -v 'resp.body.Close()' | tee /dev/stderr); done)"" \ No newline at end of file diff --git a/management/util.go b/management/util.go index 420e1374bd36..72601757e550 100644 --- a/management/util.go +++ b/management/util.go @@ -5,9 +5,7 @@ import ( "net/http" ) -func getResponseBody(response *http.Response) (out []byte, err error) { - defer func() { - err = response.Body.Close() - }() +func getResponseBody(response *http.Response) ([]byte, error) { + defer response.Body.Close() return ioutil.ReadAll(response.Body) } diff --git a/storage/blob.go b/storage/blob.go index 663871251ebc..67b2fe2bc65f 100644 --- a/storage/blob.go +++ b/storage/blob.go @@ -368,46 +368,41 @@ var ( // pagination token and other response details. // // See https://msdn.microsoft.com/en-us/library/azure/dd179352.aspx -func (b BlobStorageClient) ListContainers(params ListContainersParameters) (clr ContainerListResponse, err error) { +func (b BlobStorageClient) ListContainers(params ListContainersParameters) (ContainerListResponse, error) { q := mergeParams(params.getParameters(), url.Values{"comp": {"list"}}) uri := b.client.getEndpoint(blobServiceName, "", q) headers := b.client.getStandardHeaders() + var out ContainerListResponse resp, err := b.client.exec("GET", uri, headers, nil) if err != nil { - return clr, err + return out, err } - defer func() { - err = resp.body.Close() - }() + defer resp.body.Close() - err = xmlUnmarshal(resp.body, &clr) - return clr, err + err = xmlUnmarshal(resp.body, &out) + return out, err } // CreateContainer creates a blob container within the storage account // with given name and access level. Returns error if container already exists. // // See https://msdn.microsoft.com/en-us/library/azure/dd179468.aspx -func (b BlobStorageClient) CreateContainer(name string, access ContainerAccessType) (err error) { +func (b BlobStorageClient) CreateContainer(name string, access ContainerAccessType) error { resp, err := b.createContainer(name, access) if err != nil { return err } - defer func() { - err = resp.body.Close() - }() + defer resp.body.Close() return checkRespCode(resp.statusCode, []int{http.StatusCreated}) } // CreateContainerIfNotExists creates a blob container if it does not exist. Returns // true if container is newly created or false if container already exists. -func (b BlobStorageClient) CreateContainerIfNotExists(name string, access ContainerAccessType) (notExists bool, err error) { +func (b BlobStorageClient) CreateContainerIfNotExists(name string, access ContainerAccessType) (bool, error) { resp, err := b.createContainer(name, access) if resp != nil { - defer func() { - err = resp.body.Close() - }() + defer resp.body.Close() if resp.statusCode == http.StatusCreated || resp.statusCode == http.StatusConflict { return resp.statusCode == http.StatusCreated, nil } @@ -428,16 +423,14 @@ func (b BlobStorageClient) createContainer(name string, access ContainerAccessTy // ContainerExists returns true if a container with given name exists // on the storage account, otherwise returns false. -func (b BlobStorageClient) ContainerExists(name string) (exists bool, err error) { +func (b BlobStorageClient) ContainerExists(name string) (bool, error) { verb := "HEAD" uri := b.client.getEndpoint(blobServiceName, pathForContainer(name), url.Values{"restype": {"container"}}) headers := b.client.getStandardHeaders() resp, err := b.client.exec(verb, uri, headers, nil) if resp != nil { - defer func() { - err = resp.body.Close() - }() + defer resp.body.Close() if resp.statusCode == http.StatusOK || resp.statusCode == http.StatusNotFound { return resp.statusCode == http.StatusOK, nil } @@ -449,14 +442,12 @@ func (b BlobStorageClient) ContainerExists(name string) (exists bool, err error) // account. If the container does not exist returns error. // // See https://msdn.microsoft.com/en-us/library/azure/dd179408.aspx -func (b BlobStorageClient) DeleteContainer(name string) (err error) { +func (b BlobStorageClient) DeleteContainer(name string) error { resp, err := b.deleteContainer(name) if err != nil { return err } - defer func() { - err = resp.body.Close() - }() + defer resp.body.Close() return checkRespCode(resp.statusCode, []int{http.StatusAccepted}) } @@ -466,12 +457,10 @@ func (b BlobStorageClient) DeleteContainer(name string) (err error) { // operation. // // See https://msdn.microsoft.com/en-us/library/azure/dd179408.aspx -func (b BlobStorageClient) DeleteContainerIfExists(name string) (deleted bool, err error) { +func (b BlobStorageClient) DeleteContainerIfExists(name string) (bool, error) { resp, err := b.deleteContainer(name) if resp != nil { - defer func() { - err = resp.body.Close() - }() + defer resp.body.Close() if resp.statusCode == http.StatusAccepted || resp.statusCode == http.StatusNotFound { return resp.statusCode == http.StatusAccepted, nil } @@ -491,36 +480,33 @@ func (b BlobStorageClient) deleteContainer(name string) (*storageResponse, error // pagination token and other information in the response of List Blobs call. // // See https://msdn.microsoft.com/en-us/library/azure/dd135734.aspx -func (b BlobStorageClient) ListBlobs(container string, params ListBlobsParameters) (blr BlobListResponse, err error) { +func (b BlobStorageClient) ListBlobs(container string, params ListBlobsParameters) (BlobListResponse, error) { q := mergeParams(params.getParameters(), url.Values{ "restype": {"container"}, "comp": {"list"}}) uri := b.client.getEndpoint(blobServiceName, pathForContainer(container), q) headers := b.client.getStandardHeaders() + var out BlobListResponse resp, err := b.client.exec("GET", uri, headers, nil) if err != nil { - return blr, err + return out, err } - defer func() { - err = resp.body.Close() - }() + defer resp.body.Close() - err = xmlUnmarshal(resp.body, &blr) - return blr, err + err = xmlUnmarshal(resp.body, &out) + return out, err } // BlobExists returns true if a blob with given name exists on the specified // container of the storage account. -func (b BlobStorageClient) BlobExists(container, name string) (exists bool, err error) { +func (b BlobStorageClient) BlobExists(container, name string) (bool, error) { verb := "HEAD" uri := b.client.getEndpoint(blobServiceName, pathForBlob(container, name), url.Values{}) headers := b.client.getStandardHeaders() resp, err := b.client.exec(verb, uri, headers, nil) if resp != nil { - defer func() { - err = resp.body.Close() - }() + defer resp.body.Close() if resp.statusCode == http.StatusOK || resp.statusCode == http.StatusNotFound { return resp.statusCode == http.StatusOK, nil } @@ -592,7 +578,7 @@ func (b BlobStorageClient) getBlobRange(container, name, bytesRange string, extr } // leasePut is common PUT code for the various aquire/release/break etc functions. -func (b BlobStorageClient) leaseCommonPut(container string, name string, headers map[string]string, expectedStatus int) (h http.Header, err error) { +func (b BlobStorageClient) leaseCommonPut(container string, name string, headers map[string]string, expectedStatus int) (http.Header, error) { params := url.Values{"comp": {"lease"}} uri := b.client.getEndpoint(blobServiceName, pathForBlob(container, name), params) @@ -600,9 +586,7 @@ func (b BlobStorageClient) leaseCommonPut(container string, name string, headers if err != nil { return nil, err } - defer func() { - err = resp.body.Close() - }() + defer resp.body.Close() if err := checkRespCode(resp.statusCode, []int{expectedStatus}); err != nil { return nil, err @@ -723,7 +707,7 @@ func (b BlobStorageClient) RenewLease(container string, name string, currentLeas // GetBlobProperties provides various information about the specified // blob. See https://msdn.microsoft.com/en-us/library/azure/dd179394.aspx -func (b BlobStorageClient) GetBlobProperties(container, name string) (bp *BlobProperties, err error) { +func (b BlobStorageClient) GetBlobProperties(container, name string) (*BlobProperties, error) { verb := "HEAD" uri := b.client.getEndpoint(blobServiceName, pathForBlob(container, name), url.Values{}) @@ -732,9 +716,7 @@ func (b BlobStorageClient) GetBlobProperties(container, name string) (bp *BlobPr if err != nil { return nil, err } - defer func() { - err = resp.body.Close() - }() + defer resp.body.Close() if err := checkRespCode(resp.statusCode, []int{http.StatusOK}); err != nil { return nil, err @@ -787,7 +769,7 @@ func (b BlobStorageClient) GetBlobProperties(container, name string) (bp *BlobPr // applications either. // // See https://msdn.microsoft.com/en-us/library/azure/ee691966.aspx -func (b BlobStorageClient) SetBlobProperties(container, name string, blobHeaders BlobHeaders) (err error) { +func (b BlobStorageClient) SetBlobProperties(container, name string, blobHeaders BlobHeaders) error { params := url.Values{"comp": {"properties"}} uri := b.client.getEndpoint(blobServiceName, pathForBlob(container, name), params) headers := b.client.getStandardHeaders() @@ -802,9 +784,7 @@ func (b BlobStorageClient) SetBlobProperties(container, name string, blobHeaders if err != nil { return err } - defer func() { - err = resp.body.Close() - }() + defer resp.body.Close() return checkRespCode(resp.statusCode, []int{http.StatusOK}) } @@ -817,7 +797,7 @@ func (b BlobStorageClient) SetBlobProperties(container, name string, blobHeaders // applications either. // // See https://msdn.microsoft.com/en-us/library/azure/dd179414.aspx -func (b BlobStorageClient) SetBlobMetadata(container, name string, metadata map[string]string, extraHeaders map[string]string) (err error) { +func (b BlobStorageClient) SetBlobMetadata(container, name string, metadata map[string]string, extraHeaders map[string]string) error { params := url.Values{"comp": {"metadata"}} uri := b.client.getEndpoint(blobServiceName, pathForBlob(container, name), params) headers := b.client.getStandardHeaders() @@ -833,9 +813,7 @@ func (b BlobStorageClient) SetBlobMetadata(container, name string, metadata map[ if err != nil { return err } - defer func() { - err = resp.body.Close() - }() + defer resp.body.Close() return checkRespCode(resp.statusCode, []int{http.StatusOK}) } @@ -846,7 +824,7 @@ func (b BlobStorageClient) SetBlobMetadata(container, name string, metadata map[ // names are case-insensitive.) // // See https://msdn.microsoft.com/en-us/library/azure/dd179414.aspx -func (b BlobStorageClient) GetBlobMetadata(container, name string) (metadata map[string]string, err error) { +func (b BlobStorageClient) GetBlobMetadata(container, name string) (map[string]string, error) { params := url.Values{"comp": {"metadata"}} uri := b.client.getEndpoint(blobServiceName, pathForBlob(container, name), params) headers := b.client.getStandardHeaders() @@ -855,15 +833,13 @@ func (b BlobStorageClient) GetBlobMetadata(container, name string) (metadata map if err != nil { return nil, err } - defer func() { - err = resp.body.Close() - }() + defer resp.body.Close() if err := checkRespCode(resp.statusCode, []int{http.StatusOK}); err != nil { return nil, err } - metadata = make(map[string]string) + metadata := make(map[string]string) for k, v := range resp.headers { // Can't trust CanonicalHeaderKey() to munge case // reliably. "_" is allowed in identifiers: @@ -901,7 +877,7 @@ func (b BlobStorageClient) CreateBlockBlob(container, name string) error { // PutBlock, and PutBlockList. // // See https://msdn.microsoft.com/en-us/library/azure/dd179451.aspx -func (b BlobStorageClient) CreateBlockBlobFromReader(container, name string, size uint64, blob io.Reader, extraHeaders map[string]string) (err error) { +func (b BlobStorageClient) CreateBlockBlobFromReader(container, name string, size uint64, blob io.Reader, extraHeaders map[string]string) error { path := fmt.Sprintf("%s/%s", container, name) uri := b.client.getEndpoint(blobServiceName, path, url.Values{}) headers := b.client.getStandardHeaders() @@ -916,9 +892,7 @@ func (b BlobStorageClient) CreateBlockBlobFromReader(container, name string, siz if err != nil { return err } - defer func() { - err = resp.body.Close() - }() + defer resp.body.Close() return checkRespCode(resp.statusCode, []int{http.StatusCreated}) } @@ -941,7 +915,7 @@ func (b BlobStorageClient) PutBlock(container, name, blockID string, chunk []byt // checked by the SDK). // // See https://msdn.microsoft.com/en-us/library/azure/dd135726.aspx -func (b BlobStorageClient) PutBlockWithLength(container, name, blockID string, size uint64, blob io.Reader, extraHeaders map[string]string) (err error) { +func (b BlobStorageClient) PutBlockWithLength(container, name, blockID string, size uint64, blob io.Reader, extraHeaders map[string]string) error { uri := b.client.getEndpoint(blobServiceName, pathForBlob(container, name), url.Values{"comp": {"block"}, "blockid": {blockID}}) headers := b.client.getStandardHeaders() headers["x-ms-blob-type"] = string(BlobTypeBlock) @@ -956,16 +930,14 @@ func (b BlobStorageClient) PutBlockWithLength(container, name, blockID string, s return err } - defer func() { - err = resp.body.Close() - }() + defer resp.body.Close() return checkRespCode(resp.statusCode, []int{http.StatusCreated}) } // PutBlockList saves list of blocks to the specified block blob. // // See https://msdn.microsoft.com/en-us/library/azure/dd179467.aspx -func (b BlobStorageClient) PutBlockList(container, name string, blocks []Block) (err error) { +func (b BlobStorageClient) PutBlockList(container, name string, blocks []Block) error { blockListXML := prepareBlockListRequest(blocks) uri := b.client.getEndpoint(blobServiceName, pathForBlob(container, name), url.Values{"comp": {"blocklist"}}) @@ -976,30 +948,27 @@ func (b BlobStorageClient) PutBlockList(container, name string, blocks []Block) if err != nil { return err } - defer func() { - err = resp.body.Close() - }() + defer resp.body.Close() return checkRespCode(resp.statusCode, []int{http.StatusCreated}) } // GetBlockList retrieves list of blocks in the specified block blob. // // See https://msdn.microsoft.com/en-us/library/azure/dd179400.aspx -func (b BlobStorageClient) GetBlockList(container, name string, blockType BlockListType) (blr BlockListResponse, err error) { +func (b BlobStorageClient) GetBlockList(container, name string, blockType BlockListType) (BlockListResponse, error) { params := url.Values{"comp": {"blocklist"}, "blocklisttype": {string(blockType)}} uri := b.client.getEndpoint(blobServiceName, pathForBlob(container, name), params) headers := b.client.getStandardHeaders() + var out BlockListResponse resp, err := b.client.exec("GET", uri, headers, nil) if err != nil { - return blr, err + return out, err } - defer func() { - err = resp.body.Close() - }() + defer resp.body.Close() - err = xmlUnmarshal(resp.body, &blr) - return blr, err + err = xmlUnmarshal(resp.body, &out) + return out, err } // PutPageBlob initializes an empty page blob with specified name and maximum @@ -1007,7 +976,7 @@ func (b BlobStorageClient) GetBlockList(container, name string, blockType BlockL // be created using this method before writing pages. // // See https://msdn.microsoft.com/en-us/library/azure/dd179451.aspx -func (b BlobStorageClient) PutPageBlob(container, name string, size int64, extraHeaders map[string]string) (err error) { +func (b BlobStorageClient) PutPageBlob(container, name string, size int64, extraHeaders map[string]string) error { path := fmt.Sprintf("%s/%s", container, name) uri := b.client.getEndpoint(blobServiceName, path, url.Values{}) headers := b.client.getStandardHeaders() @@ -1022,9 +991,7 @@ func (b BlobStorageClient) PutPageBlob(container, name string, size int64, extra if err != nil { return err } - defer func() { - err = resp.body.Close() - }() + defer resp.body.Close() return checkRespCode(resp.statusCode, []int{http.StatusCreated}) } @@ -1034,7 +1001,7 @@ func (b BlobStorageClient) PutPageBlob(container, name string, size int64, extra // with 512-byte boundaries and chunk must be of size multiplies by 512. // // See https://msdn.microsoft.com/en-us/library/ee691975.aspx -func (b BlobStorageClient) PutPage(container, name string, startByte, endByte int64, writeType PageWriteType, chunk []byte, extraHeaders map[string]string) (err error) { +func (b BlobStorageClient) PutPage(container, name string, startByte, endByte int64, writeType PageWriteType, chunk []byte, extraHeaders map[string]string) error { path := fmt.Sprintf("%s/%s", container, name) uri := b.client.getEndpoint(blobServiceName, path, url.Values{"comp": {"page"}}) headers := b.client.getStandardHeaders() @@ -1059,9 +1026,7 @@ func (b BlobStorageClient) PutPage(container, name string, startByte, endByte in if err != nil { return err } - defer func() { - err = resp.body.Close() - }() + defer resp.body.Close() return checkRespCode(resp.statusCode, []int{http.StatusCreated}) } @@ -1069,31 +1034,30 @@ func (b BlobStorageClient) PutPage(container, name string, startByte, endByte in // GetPageRanges returns the list of valid page ranges for a page blob. // // See https://msdn.microsoft.com/en-us/library/azure/ee691973.aspx -func (b BlobStorageClient) GetPageRanges(container, name string) (pageRanges GetPageRangesResponse, err error) { +func (b BlobStorageClient) GetPageRanges(container, name string) (GetPageRangesResponse, error) { path := fmt.Sprintf("%s/%s", container, name) uri := b.client.getEndpoint(blobServiceName, path, url.Values{"comp": {"pagelist"}}) headers := b.client.getStandardHeaders() + var out GetPageRangesResponse resp, err := b.client.exec("GET", uri, headers, nil) if err != nil { - return pageRanges, err + return out, err } - defer func() { - err = resp.body.Close() - }() + defer resp.body.Close() if err := checkRespCode(resp.statusCode, []int{http.StatusOK}); err != nil { - return pageRanges, err + return out, err } - err = xmlUnmarshal(resp.body, &pageRanges) - return pageRanges, err + err = xmlUnmarshal(resp.body, &out) + return out, err } // PutAppendBlob initializes an empty append blob with specified name. An // append blob must be created using this method before appending blocks. // // See https://msdn.microsoft.com/en-us/library/azure/dd179451.aspx -func (b BlobStorageClient) PutAppendBlob(container, name string, extraHeaders map[string]string) (err error) { +func (b BlobStorageClient) PutAppendBlob(container, name string, extraHeaders map[string]string) error { path := fmt.Sprintf("%s/%s", container, name) uri := b.client.getEndpoint(blobServiceName, path, url.Values{}) headers := b.client.getStandardHeaders() @@ -1107,9 +1071,7 @@ func (b BlobStorageClient) PutAppendBlob(container, name string, extraHeaders ma if err != nil { return err } - defer func() { - err = resp.body.Close() - }() + defer resp.body.Close() return checkRespCode(resp.statusCode, []int{http.StatusCreated}) } @@ -1117,7 +1079,7 @@ func (b BlobStorageClient) PutAppendBlob(container, name string, extraHeaders ma // AppendBlock appends a block to an append blob. // // See https://msdn.microsoft.com/en-us/library/azure/mt427365.aspx -func (b BlobStorageClient) AppendBlock(container, name string, chunk []byte, extraHeaders map[string]string) (err error) { +func (b BlobStorageClient) AppendBlock(container, name string, chunk []byte, extraHeaders map[string]string) error { path := fmt.Sprintf("%s/%s", container, name) uri := b.client.getEndpoint(blobServiceName, path, url.Values{"comp": {"appendblock"}}) headers := b.client.getStandardHeaders() @@ -1132,9 +1094,7 @@ func (b BlobStorageClient) AppendBlock(container, name string, chunk []byte, ext if err != nil { return err } - defer func() { - err = resp.body.Close() - }() + defer resp.body.Close() return checkRespCode(resp.statusCode, []int{http.StatusCreated}) } @@ -1154,7 +1114,7 @@ func (b BlobStorageClient) CopyBlob(container, name, sourceBlob string) error { return b.waitForBlobCopy(container, name, copyID) } -func (b BlobStorageClient) startBlobCopy(container, name, sourceBlob string) (copyID string, err error) { +func (b BlobStorageClient) startBlobCopy(container, name, sourceBlob string) (string, error) { uri := b.client.getEndpoint(blobServiceName, pathForBlob(container, name), url.Values{}) headers := b.client.getStandardHeaders() @@ -1164,15 +1124,13 @@ func (b BlobStorageClient) startBlobCopy(container, name, sourceBlob string) (co if err != nil { return "", err } - defer func() { - err = resp.body.Close() - }() + defer resp.body.Close() if err := checkRespCode(resp.statusCode, []int{http.StatusAccepted, http.StatusCreated}); err != nil { return "", err } - copyID = resp.headers.Get("x-ms-copy-id") + copyID := resp.headers.Get("x-ms-copy-id") if copyID == "" { return "", errors.New("Got empty copy id header") } @@ -1208,14 +1166,12 @@ func (b BlobStorageClient) waitForBlobCopy(container, name, copyID string) error // DeleteBlob deletes the given blob from the specified container. // If the blob does not exists at the time of the Delete Blob operation, it // returns error. See https://msdn.microsoft.com/en-us/library/azure/dd179413.aspx -func (b BlobStorageClient) DeleteBlob(container, name string, extraHeaders map[string]string) (err error) { +func (b BlobStorageClient) DeleteBlob(container, name string, extraHeaders map[string]string) error { resp, err := b.deleteBlob(container, name, extraHeaders) if err != nil { return err } - defer func() { - err = resp.body.Close() - }() + defer resp.body.Close() return checkRespCode(resp.statusCode, []int{http.StatusAccepted}) } @@ -1223,14 +1179,12 @@ func (b BlobStorageClient) DeleteBlob(container, name string, extraHeaders map[s // blob is deleted with this call, returns true. Otherwise returns false. // // See https://msdn.microsoft.com/en-us/library/azure/dd179413.aspx -func (b BlobStorageClient) DeleteBlobIfExists(container, name string, extraHeaders map[string]string) (deleted bool, err error) { +func (b BlobStorageClient) DeleteBlobIfExists(container, name string, extraHeaders map[string]string) (bool, error) { resp, err := b.deleteBlob(container, name, extraHeaders) if resp != nil && (resp.statusCode == http.StatusAccepted || resp.statusCode == http.StatusNotFound) { return resp.statusCode == http.StatusAccepted, nil } - defer func() { - err = resp.body.Close() - }() + defer resp.body.Close() return false, err } diff --git a/storage/client.go b/storage/client.go index 670c22785103..60e0a0baff3e 100644 --- a/storage/client.go +++ b/storage/client.go @@ -521,11 +521,9 @@ func (c Client) execTable(verb, url string, headers map[string]string, body io.R return c.execInternalJSON(verb, url, headers, body) } -func readResponseBody(resp *http.Response) (out []byte, err error) { - defer func() { - err = resp.Body.Close() - }() - out, err = ioutil.ReadAll(resp.Body) +func readResponseBody(resp *http.Response) ([]byte, error) { + defer resp.Body.Close() + out, err := ioutil.ReadAll(resp.Body) if err == io.EOF { err = nil } diff --git a/storage/file.go b/storage/file.go index bbc0230ba63d..2397587c886d 100644 --- a/storage/file.go +++ b/storage/file.go @@ -90,35 +90,32 @@ func pathForFileShare(name string) string { // pagination token and other response details. // // See https://msdn.microsoft.com/en-us/library/azure/dd179352.aspx -func (f FileServiceClient) ListShares(params ListSharesParameters) (slr ShareListResponse, err error) { +func (f FileServiceClient) ListShares(params ListSharesParameters) (ShareListResponse, error) { q := mergeParams(params.getParameters(), url.Values{"comp": {"list"}}) uri := f.client.getEndpoint(fileServiceName, "", q) headers := f.client.getStandardHeaders() + var out ShareListResponse resp, err := f.client.exec("GET", uri, headers, nil) if err != nil { - return slr, err + return out, err } - defer func() { - err = resp.body.Close() - }() + defer resp.body.Close() - err = xmlUnmarshal(resp.body, &slr) - return slr, err + err = xmlUnmarshal(resp.body, &out) + return out, err } // CreateShare operation creates a new share under the specified account. If the // share with the same name already exists, the operation fails. // // See https://msdn.microsoft.com/en-us/library/azure/dn167008.aspx -func (f FileServiceClient) CreateShare(name string) (err error) { +func (f FileServiceClient) CreateShare(name string) error { resp, err := f.createShare(name) if err != nil { return err } - defer func() { - err = resp.body.Close() - }() + defer resp.body.Close() return checkRespCode(resp.statusCode, []int{http.StatusCreated}) } @@ -130,9 +127,7 @@ func (f FileServiceClient) ShareExists(name string) (bool, error) { resp, err := f.client.exec("HEAD", uri, headers, nil) if resp != nil { - defer func() { - err = resp.body.Close() - }() + defer resp.body.Close() if resp.statusCode == http.StatusOK || resp.statusCode == http.StatusNotFound { return resp.statusCode == http.StatusOK, nil } @@ -153,12 +148,10 @@ func (f FileServiceClient) GetShareURL(name string) string { // container already exists. // // See https://msdn.microsoft.com/en-us/library/azure/dn167008.aspx -func (f FileServiceClient) CreateShareIfNotExists(name string) (created bool, err error) { +func (f FileServiceClient) CreateShareIfNotExists(name string) (bool, error) { resp, err := f.createShare(name) if resp != nil { - defer func() { - err = resp.body.Close() - }() + defer resp.body.Close() if resp.statusCode == http.StatusCreated || resp.statusCode == http.StatusConflict { return resp.statusCode == http.StatusCreated, nil } @@ -178,7 +171,7 @@ func (f FileServiceClient) createShare(name string) (*storageResponse, error) { // GetShareProperties provides various information about the specified // file. See https://msdn.microsoft.com/en-us/library/azure/dn689099.aspx -func (f FileServiceClient) GetShareProperties(name string) (sp *ShareProperties, err error) { +func (f FileServiceClient) GetShareProperties(name string) (*ShareProperties, error) { uri := f.client.getEndpoint(fileServiceName, pathForFileShare(name), url.Values{"restype": {"share"}}) headers := f.client.getStandardHeaders() @@ -186,9 +179,7 @@ func (f FileServiceClient) GetShareProperties(name string) (sp *ShareProperties, if err != nil { return nil, err } - defer func() { - err = resp.body.Close() - }() + defer resp.body.Close() if err := checkRespCode(resp.statusCode, []int{http.StatusOK}); err != nil { return nil, err @@ -209,7 +200,7 @@ func (f FileServiceClient) GetShareProperties(name string) (sp *ShareProperties, // applications either. // // See https://msdn.microsoft.com/en-us/library/azure/mt427368.aspx -func (f FileServiceClient) SetShareProperties(name string, shareHeaders ShareHeaders) (err error) { +func (f FileServiceClient) SetShareProperties(name string, shareHeaders ShareHeaders) error { params := url.Values{} params.Set("restype", "share") params.Set("comp", "properties") @@ -227,9 +218,7 @@ func (f FileServiceClient) SetShareProperties(name string, shareHeaders ShareHea if err != nil { return err } - defer func() { - err = resp.body.Close() - }() + defer resp.body.Close() return checkRespCode(resp.statusCode, []int{http.StatusOK}) } @@ -239,14 +228,12 @@ func (f FileServiceClient) SetShareProperties(name string, shareHeaders ShareHea // collection. // // See https://msdn.microsoft.com/en-us/library/azure/dn689090.aspx -func (f FileServiceClient) DeleteShare(name string) (err error) { +func (f FileServiceClient) DeleteShare(name string) error { resp, err := f.deleteShare(name) if err != nil { return err } - defer func() { - err = resp.body.Close() - }() + defer resp.body.Close() return checkRespCode(resp.statusCode, []int{http.StatusAccepted}) } @@ -256,12 +243,10 @@ func (f FileServiceClient) DeleteShare(name string) (err error) { // false otherwise. // // See https://msdn.microsoft.com/en-us/library/azure/dn689090.aspx -func (f FileServiceClient) DeleteShareIfExists(name string) (deleted bool, err error) { +func (f FileServiceClient) DeleteShareIfExists(name string) (bool, error) { resp, err := f.deleteShare(name) if resp != nil { - defer func() { - err = resp.body.Close() - }() + defer resp.body.Close() if resp.statusCode == http.StatusAccepted || resp.statusCode == http.StatusNotFound { return resp.statusCode == http.StatusAccepted, nil } @@ -287,7 +272,7 @@ func (f FileServiceClient) deleteShare(name string) (*storageResponse, error) { // applications either. // // See https://msdn.microsoft.com/en-us/library/azure/dd179414.aspx -func (f FileServiceClient) SetShareMetadata(name string, metadata map[string]string, extraHeaders map[string]string) (err error) { +func (f FileServiceClient) SetShareMetadata(name string, metadata map[string]string, extraHeaders map[string]string) error { params := url.Values{} params.Set("restype", "share") params.Set("comp", "metadata") @@ -306,9 +291,7 @@ func (f FileServiceClient) SetShareMetadata(name string, metadata map[string]str if err != nil { return err } - defer func() { - err = resp.body.Close() - }() + defer resp.body.Close() return checkRespCode(resp.statusCode, []int{http.StatusOK}) } @@ -319,7 +302,7 @@ func (f FileServiceClient) SetShareMetadata(name string, metadata map[string]str // names are case-insensitive.) // // See https://msdn.microsoft.com/en-us/library/azure/dd179414.aspx -func (f FileServiceClient) GetShareMetadata(name string) (metadata map[string]string, err error) { +func (f FileServiceClient) GetShareMetadata(name string) (map[string]string, error) { params := url.Values{} params.Set("restype", "share") params.Set("comp", "metadata") @@ -331,15 +314,13 @@ func (f FileServiceClient) GetShareMetadata(name string) (metadata map[string]st if err != nil { return nil, err } - defer func() { - err = resp.body.Close() - }() + defer resp.body.Close() if err := checkRespCode(resp.statusCode, []int{http.StatusOK}); err != nil { return nil, err } - metadata = make(map[string]string) + metadata := make(map[string]string) for k, v := range resp.headers { // Can't trust CanonicalHeaderKey() to munge case // reliably. "_" is allowed in identifiers: diff --git a/storage/queue.go b/storage/queue.go index 567882b858b9..3ecf4aca0d21 100644 --- a/storage/queue.go +++ b/storage/queue.go @@ -131,7 +131,7 @@ type QueueMetadataResponse struct { // Metadata is associated with the queue as name-value pairs. // // See https://msdn.microsoft.com/en-us/library/azure/dd179348.aspx -func (c QueueServiceClient) SetMetadata(name string, metadata map[string]string) (err error) { +func (c QueueServiceClient) SetMetadata(name string, metadata map[string]string) error { uri := c.client.getEndpoint(queueServiceName, pathForQueue(name), url.Values{"comp": []string{"metadata"}}) headers := c.client.getStandardHeaders() for k, v := range metadata { @@ -142,9 +142,7 @@ func (c QueueServiceClient) SetMetadata(name string, metadata map[string]string) if err != nil { return err } - defer func() { - err = resp.body.Close() - }() + defer resp.body.Close() return checkRespCode(resp.statusCode, []int{http.StatusNoContent}) } @@ -158,7 +156,7 @@ func (c QueueServiceClient) SetMetadata(name string, metadata map[string]string) // Because the way Golang's http client (and http.Header in particular) // canonicalize header names, the returned metadata names would always // be all lower case. -func (c QueueServiceClient) GetMetadata(name string) (qmr QueueMetadataResponse, err error) { +func (c QueueServiceClient) GetMetadata(name string) (QueueMetadataResponse, error) { qm := QueueMetadataResponse{} qm.UserDefinedMetadata = make(map[string]string) uri := c.client.getEndpoint(queueServiceName, pathForQueue(name), url.Values{"comp": []string{"metadata"}}) @@ -167,9 +165,7 @@ func (c QueueServiceClient) GetMetadata(name string) (qmr QueueMetadataResponse, if err != nil { return qm, err } - defer func() { - err = resp.body.Close() - }() + defer resp.body.Close() for k, v := range resp.headers { if len(v) != 1 { @@ -195,31 +191,27 @@ func (c QueueServiceClient) GetMetadata(name string) (qmr QueueMetadataResponse, // CreateQueue operation creates a queue under the given account. // // See https://msdn.microsoft.com/en-us/library/azure/dd179342.aspx -func (c QueueServiceClient) CreateQueue(name string) (err error) { +func (c QueueServiceClient) CreateQueue(name string) error { uri := c.client.getEndpoint(queueServiceName, pathForQueue(name), url.Values{}) headers := c.client.getStandardHeaders() resp, err := c.client.exec("PUT", uri, headers, nil) if err != nil { return err } - defer func() { - err = resp.body.Close() - }() + defer resp.body.Close() return checkRespCode(resp.statusCode, []int{http.StatusCreated}) } // DeleteQueue operation permanently deletes the specified queue. // // See https://msdn.microsoft.com/en-us/library/azure/dd179436.aspx -func (c QueueServiceClient) DeleteQueue(name string) (err error) { +func (c QueueServiceClient) DeleteQueue(name string) error { uri := c.client.getEndpoint(queueServiceName, pathForQueue(name), url.Values{}) resp, err := c.client.exec("DELETE", uri, c.client.getStandardHeaders(), nil) if err != nil { return err } - defer func() { - err = resp.body.Close() - }() + defer resp.body.Close() return checkRespCode(resp.statusCode, []int{http.StatusNoContent}) } @@ -237,7 +229,7 @@ func (c QueueServiceClient) QueueExists(name string) (bool, error) { // PutMessage operation adds a new message to the back of the message queue. // // See https://msdn.microsoft.com/en-us/library/azure/dd179346.aspx -func (c QueueServiceClient) PutMessage(queue string, message string, params PutMessageParameters) (err error) { +func (c QueueServiceClient) PutMessage(queue string, message string, params PutMessageParameters) error { uri := c.client.getEndpoint(queueServiceName, pathForQueueMessages(queue), params.getParameters()) req := putMessageRequest{MessageText: message} body, nn, err := xmlMarshal(req) @@ -250,24 +242,20 @@ func (c QueueServiceClient) PutMessage(queue string, message string, params PutM if err != nil { return err } - defer func() { - err = resp.body.Close() - }() + defer resp.body.Close() return checkRespCode(resp.statusCode, []int{http.StatusCreated}) } // ClearMessages operation deletes all messages from the specified queue. // // See https://msdn.microsoft.com/en-us/library/azure/dd179454.aspx -func (c QueueServiceClient) ClearMessages(queue string) (err error) { +func (c QueueServiceClient) ClearMessages(queue string) error { uri := c.client.getEndpoint(queueServiceName, pathForQueueMessages(queue), url.Values{}) resp, err := c.client.exec("DELETE", uri, c.client.getStandardHeaders(), nil) if err != nil { return err } - defer func() { - err = resp.body.Close() - }() + defer resp.body.Close() return checkRespCode(resp.statusCode, []int{http.StatusNoContent}) } @@ -275,48 +263,44 @@ func (c QueueServiceClient) ClearMessages(queue string) (err error) { // queue. // // See https://msdn.microsoft.com/en-us/library/azure/dd179474.aspx -func (c QueueServiceClient) GetMessages(queue string, params GetMessagesParameters) (gmr GetMessagesResponse, err error) { +func (c QueueServiceClient) GetMessages(queue string, params GetMessagesParameters) (GetMessagesResponse, error) { + var r GetMessagesResponse uri := c.client.getEndpoint(queueServiceName, pathForQueueMessages(queue), params.getParameters()) resp, err := c.client.exec("GET", uri, c.client.getStandardHeaders(), nil) if err != nil { - return gmr, err + return r, err } - defer func() { - err = resp.body.Close() - }() - err = xmlUnmarshal(resp.body, &gmr) - return gmr, err + defer resp.body.Close() + err = xmlUnmarshal(resp.body, &r) + return r, err } // PeekMessages retrieves one or more messages from the front of the queue, but // does not alter the visibility of the message. // // See https://msdn.microsoft.com/en-us/library/azure/dd179472.aspx -func (c QueueServiceClient) PeekMessages(queue string, params PeekMessagesParameters) (pmr PeekMessagesResponse, err error) { +func (c QueueServiceClient) PeekMessages(queue string, params PeekMessagesParameters) (PeekMessagesResponse, error) { + var r PeekMessagesResponse uri := c.client.getEndpoint(queueServiceName, pathForQueueMessages(queue), params.getParameters()) resp, err := c.client.exec("GET", uri, c.client.getStandardHeaders(), nil) if err != nil { - return pmr, err + return r, err } - defer func() { - err = resp.body.Close() - }() - err = xmlUnmarshal(resp.body, &pmr) - return pmr, err + defer resp.body.Close() + err = xmlUnmarshal(resp.body, &r) + return r, err } // DeleteMessage operation deletes the specified message. // // See https://msdn.microsoft.com/en-us/library/azure/dd179347.aspx -func (c QueueServiceClient) DeleteMessage(queue, messageID, popReceipt string) (err error) { +func (c QueueServiceClient) DeleteMessage(queue, messageID, popReceipt string) error { uri := c.client.getEndpoint(queueServiceName, pathForMessage(queue, messageID), url.Values{ "popreceipt": {popReceipt}}) resp, err := c.client.exec("DELETE", uri, c.client.getStandardHeaders(), nil) if err != nil { return err } - defer func() { - err = resp.body.Close() - }() + defer resp.body.Close() return checkRespCode(resp.statusCode, []int{http.StatusNoContent}) } diff --git a/storage/table.go b/storage/table.go index 88be0f517764..0826e78672ca 100644 --- a/storage/table.go +++ b/storage/table.go @@ -39,7 +39,7 @@ func (c *TableServiceClient) getStandardHeaders() map[string]string { // QueryTables returns the tables created in the // *TableServiceClient storage account. -func (c *TableServiceClient) QueryTables() (at []AzureTable, err error) { +func (c *TableServiceClient) QueryTables() ([]AzureTable, error) { uri := c.client.getEndpoint(tableServiceName, tablesURIPath, url.Values{}) headers := c.getStandardHeaders() @@ -49,9 +49,7 @@ func (c *TableServiceClient) QueryTables() (at []AzureTable, err error) { if err != nil { return nil, err } - defer func() { - err = resp.body.Close() - }() + defer resp.body.Close() if err := checkRespCode(resp.statusCode, []int{http.StatusOK}); err != nil { return nil, err @@ -67,18 +65,18 @@ func (c *TableServiceClient) QueryTables() (at []AzureTable, err error) { return nil, err } - at = make([]AzureTable, len(respArray.TableName)) + s := make([]AzureTable, len(respArray.TableName)) for i, elem := range respArray.TableName { - at[i] = AzureTable(elem.TableName) + s[i] = AzureTable(elem.TableName) } - return at, nil + return s, nil } // CreateTable creates the table given the specific // name. This function fails if the name is not compliant // with the specification or the tables already exists. -func (c *TableServiceClient) CreateTable(table AzureTable) (err error) { +func (c *TableServiceClient) CreateTable(table AzureTable) error { uri := c.client.getEndpoint(tableServiceName, tablesURIPath, url.Values{}) headers := c.getStandardHeaders() @@ -97,9 +95,7 @@ func (c *TableServiceClient) CreateTable(table AzureTable) (err error) { if err != nil { return err } - defer func() { - err = resp.body.Close() - }() + defer resp.body.Close() if err := checkRespCode(resp.statusCode, []int{http.StatusCreated}); err != nil { return err @@ -112,7 +108,7 @@ func (c *TableServiceClient) CreateTable(table AzureTable) (err error) { // name. This function fails if the table is not present. // Be advised: DeleteTable deletes all the entries // that may be present. -func (c *TableServiceClient) DeleteTable(table AzureTable) (err error) { +func (c *TableServiceClient) DeleteTable(table AzureTable) error { uri := c.client.getEndpoint(tableServiceName, tablesURIPath, url.Values{}) uri += fmt.Sprintf("('%s')", string(table)) @@ -125,9 +121,7 @@ func (c *TableServiceClient) DeleteTable(table AzureTable) (err error) { if err != nil { return err } - defer func() { - err = resp.body.Close() - }() + defer resp.body.Close() if err := checkRespCode(resp.statusCode, []int{http.StatusNoContent}); err != nil { return err diff --git a/storage/table_entities.go b/storage/table_entities.go index fd201a65517e..3d4bf501ea03 100644 --- a/storage/table_entities.go +++ b/storage/table_entities.go @@ -77,7 +77,7 @@ type getTableEntriesResponse struct { // // Example: // entities, cToken, err = tSvc.QueryTableEntities("table", cToken, reflect.TypeOf(entity), 20, "") -func (c *TableServiceClient) QueryTableEntities(tableName AzureTable, previousContToken *ContinuationToken, retType reflect.Type, top int, query string) (te []TableEntity, ct *ContinuationToken, err error) { +func (c *TableServiceClient) QueryTableEntities(tableName AzureTable, previousContToken *ContinuationToken, retType reflect.Type, top int, query string) ([]TableEntity, *ContinuationToken, error) { if top > maxTopParameter { return nil, nil, fmt.Errorf("top accepts at maximum %d elements. Requested %d instead", maxTopParameter, top) } @@ -107,9 +107,7 @@ func (c *TableServiceClient) QueryTableEntities(tableName AzureTable, previousCo if err != nil { return nil, contToken, err } - defer func() { - err = resp.body.Close() - }() + defer resp.body.Close() if err := checkRespCode(resp.statusCode, []int{http.StatusOK}); err != nil { return nil, contToken, err @@ -136,7 +134,7 @@ func (c *TableServiceClient) InsertEntity(table AzureTable, entity TableEntity) return err } -func (c *TableServiceClient) execTable(table AzureTable, entity TableEntity, specifyKeysInURL bool, method string) (statusCode int, err error) { +func (c *TableServiceClient) execTable(table AzureTable, entity TableEntity, specifyKeysInURL bool, method string) (int, error) { uri := c.client.getEndpoint(tableServiceName, pathForTable(table), url.Values{}) if specifyKeysInURL { uri += fmt.Sprintf("(PartitionKey='%s',RowKey='%s')", url.QueryEscape(entity.PartitionKey()), url.QueryEscape(entity.RowKey())) @@ -158,9 +156,7 @@ func (c *TableServiceClient) execTable(table AzureTable, entity TableEntity, spe return 0, err } - defer func() { - err = resp.body.Close() - }() + defer resp.body.Close() return resp.statusCode, nil } @@ -218,9 +214,7 @@ func (c *TableServiceClient) DeleteEntity(table AzureTable, entity TableEntity, if err != nil { return err } - defer func() { - err = resp.body.Close() - }() + defer resp.body.Close() if err := checkRespCode(resp.statusCode, []int{http.StatusNoContent}); err != nil { return err From 9048767a6027331daf6b10b0e45cddee883721f3 Mon Sep 17 00:00:00 2001 From: mcardosos Date: Thu, 8 Dec 2016 16:18:13 -0800 Subject: [PATCH 09/14] Discarding defer errors --- .travis.yml | 2 +- storage/blob.go | 100 ++++++++++++++++++++++++++++---------- storage/client.go | 4 +- storage/file.go | 40 +++++++++++---- storage/queue.go | 36 ++++++++++---- storage/table.go | 12 +++-- storage/table_entities.go | 12 +++-- 7 files changed, 154 insertions(+), 52 deletions(-) diff --git a/.travis.yml b/.travis.yml index 7512a9f4ea82..2fd865605724 100644 --- a/.travis.yml +++ b/.travis.yml @@ -32,6 +32,6 @@ script: - go vet ./management/... - test -z "$(golint ./Gododir/... | tee /dev/stderr)" - go vet ./Gododir/... - - test -z "$(set -e; for v in ./arm... ./datalake-store/... ./Gododir/...; do (errcheck -ignoretests "$v" | grep -v 'resp.body.Close()' | tee /dev/stderr); done)"" + - set -e; for v in ./arm/... ./datalake-store/... ./Gododir/... ./management/... ./storage/...; do errcheck -ignoretests "$v"; done \ No newline at end of file diff --git a/storage/blob.go b/storage/blob.go index 67b2fe2bc65f..bfa7c9d82a1d 100644 --- a/storage/blob.go +++ b/storage/blob.go @@ -378,7 +378,9 @@ func (b BlobStorageClient) ListContainers(params ListContainersParameters) (Cont if err != nil { return out, err } - defer resp.body.Close() + defer func() { + _ = resp.body.Close() + }() err = xmlUnmarshal(resp.body, &out) return out, err @@ -393,7 +395,9 @@ func (b BlobStorageClient) CreateContainer(name string, access ContainerAccessTy if err != nil { return err } - defer resp.body.Close() + defer func() { + _ = resp.body.Close() + }() return checkRespCode(resp.statusCode, []int{http.StatusCreated}) } @@ -402,7 +406,9 @@ func (b BlobStorageClient) CreateContainer(name string, access ContainerAccessTy func (b BlobStorageClient) CreateContainerIfNotExists(name string, access ContainerAccessType) (bool, error) { resp, err := b.createContainer(name, access) if resp != nil { - defer resp.body.Close() + defer func() { + _ = resp.body.Close() + }() if resp.statusCode == http.StatusCreated || resp.statusCode == http.StatusConflict { return resp.statusCode == http.StatusCreated, nil } @@ -430,7 +436,9 @@ func (b BlobStorageClient) ContainerExists(name string) (bool, error) { resp, err := b.client.exec(verb, uri, headers, nil) if resp != nil { - defer resp.body.Close() + defer func() { + _ = resp.body.Close() + }() if resp.statusCode == http.StatusOK || resp.statusCode == http.StatusNotFound { return resp.statusCode == http.StatusOK, nil } @@ -447,7 +455,9 @@ func (b BlobStorageClient) DeleteContainer(name string) error { if err != nil { return err } - defer resp.body.Close() + defer func() { + _ = resp.body.Close() + }() return checkRespCode(resp.statusCode, []int{http.StatusAccepted}) } @@ -460,7 +470,9 @@ func (b BlobStorageClient) DeleteContainer(name string) error { func (b BlobStorageClient) DeleteContainerIfExists(name string) (bool, error) { resp, err := b.deleteContainer(name) if resp != nil { - defer resp.body.Close() + defer func() { + _ = resp.body.Close() + }() if resp.statusCode == http.StatusAccepted || resp.statusCode == http.StatusNotFound { return resp.statusCode == http.StatusAccepted, nil } @@ -492,7 +504,9 @@ func (b BlobStorageClient) ListBlobs(container string, params ListBlobsParameter if err != nil { return out, err } - defer resp.body.Close() + defer func() { + _ = resp.body.Close() + }() err = xmlUnmarshal(resp.body, &out) return out, err @@ -506,7 +520,9 @@ func (b BlobStorageClient) BlobExists(container, name string) (bool, error) { headers := b.client.getStandardHeaders() resp, err := b.client.exec(verb, uri, headers, nil) if resp != nil { - defer resp.body.Close() + defer func() { + _ = resp.body.Close() + }() if resp.statusCode == http.StatusOK || resp.statusCode == http.StatusNotFound { return resp.statusCode == http.StatusOK, nil } @@ -586,7 +602,9 @@ func (b BlobStorageClient) leaseCommonPut(container string, name string, headers if err != nil { return nil, err } - defer resp.body.Close() + defer func() { + _ = resp.body.Close() + }() if err := checkRespCode(resp.statusCode, []int{expectedStatus}); err != nil { return nil, err @@ -716,7 +734,9 @@ func (b BlobStorageClient) GetBlobProperties(container, name string) (*BlobPrope if err != nil { return nil, err } - defer resp.body.Close() + defer func() { + _ = resp.body.Close() + }() if err := checkRespCode(resp.statusCode, []int{http.StatusOK}); err != nil { return nil, err @@ -784,7 +804,9 @@ func (b BlobStorageClient) SetBlobProperties(container, name string, blobHeaders if err != nil { return err } - defer resp.body.Close() + defer func() { + _ = resp.body.Close() + }() return checkRespCode(resp.statusCode, []int{http.StatusOK}) } @@ -813,7 +835,9 @@ func (b BlobStorageClient) SetBlobMetadata(container, name string, metadata map[ if err != nil { return err } - defer resp.body.Close() + defer func() { + _ = resp.body.Close() + }() return checkRespCode(resp.statusCode, []int{http.StatusOK}) } @@ -833,7 +857,9 @@ func (b BlobStorageClient) GetBlobMetadata(container, name string) (map[string]s if err != nil { return nil, err } - defer resp.body.Close() + defer func() { + _ = resp.body.Close() + }() if err := checkRespCode(resp.statusCode, []int{http.StatusOK}); err != nil { return nil, err @@ -892,7 +918,9 @@ func (b BlobStorageClient) CreateBlockBlobFromReader(container, name string, siz if err != nil { return err } - defer resp.body.Close() + defer func() { + _ = resp.body.Close() + }() return checkRespCode(resp.statusCode, []int{http.StatusCreated}) } @@ -930,7 +958,9 @@ func (b BlobStorageClient) PutBlockWithLength(container, name, blockID string, s return err } - defer resp.body.Close() + defer func() { + _ = resp.body.Close() + }() return checkRespCode(resp.statusCode, []int{http.StatusCreated}) } @@ -948,7 +978,9 @@ func (b BlobStorageClient) PutBlockList(container, name string, blocks []Block) if err != nil { return err } - defer resp.body.Close() + defer func() { + _ = resp.body.Close() + }() return checkRespCode(resp.statusCode, []int{http.StatusCreated}) } @@ -965,7 +997,9 @@ func (b BlobStorageClient) GetBlockList(container, name string, blockType BlockL if err != nil { return out, err } - defer resp.body.Close() + defer func() { + _ = resp.body.Close() + }() err = xmlUnmarshal(resp.body, &out) return out, err @@ -991,7 +1025,9 @@ func (b BlobStorageClient) PutPageBlob(container, name string, size int64, extra if err != nil { return err } - defer resp.body.Close() + defer func() { + _ = resp.body.Close() + }() return checkRespCode(resp.statusCode, []int{http.StatusCreated}) } @@ -1026,7 +1062,9 @@ func (b BlobStorageClient) PutPage(container, name string, startByte, endByte in if err != nil { return err } - defer resp.body.Close() + defer func() { + _ = resp.body.Close() + }() return checkRespCode(resp.statusCode, []int{http.StatusCreated}) } @@ -1044,7 +1082,9 @@ func (b BlobStorageClient) GetPageRanges(container, name string) (GetPageRangesR if err != nil { return out, err } - defer resp.body.Close() + defer func() { + _ = resp.body.Close() + }() if err := checkRespCode(resp.statusCode, []int{http.StatusOK}); err != nil { return out, err @@ -1071,7 +1111,9 @@ func (b BlobStorageClient) PutAppendBlob(container, name string, extraHeaders ma if err != nil { return err } - defer resp.body.Close() + defer func() { + _ = resp.body.Close() + }() return checkRespCode(resp.statusCode, []int{http.StatusCreated}) } @@ -1094,7 +1136,9 @@ func (b BlobStorageClient) AppendBlock(container, name string, chunk []byte, ext if err != nil { return err } - defer resp.body.Close() + defer func() { + _ = resp.body.Close() + }() return checkRespCode(resp.statusCode, []int{http.StatusCreated}) } @@ -1124,7 +1168,9 @@ func (b BlobStorageClient) startBlobCopy(container, name, sourceBlob string) (st if err != nil { return "", err } - defer resp.body.Close() + defer func() { + _ = resp.body.Close() + }() if err := checkRespCode(resp.statusCode, []int{http.StatusAccepted, http.StatusCreated}); err != nil { return "", err @@ -1171,7 +1217,9 @@ func (b BlobStorageClient) DeleteBlob(container, name string, extraHeaders map[s if err != nil { return err } - defer resp.body.Close() + defer func() { + _ = resp.body.Close() + }() return checkRespCode(resp.statusCode, []int{http.StatusAccepted}) } @@ -1184,7 +1232,9 @@ func (b BlobStorageClient) DeleteBlobIfExists(container, name string, extraHeade if resp != nil && (resp.statusCode == http.StatusAccepted || resp.statusCode == http.StatusNotFound) { return resp.statusCode == http.StatusAccepted, nil } - defer resp.body.Close() + defer func() { + _ = resp.body.Close() + }() return false, err } diff --git a/storage/client.go b/storage/client.go index 60e0a0baff3e..5d4b66c5b7f9 100644 --- a/storage/client.go +++ b/storage/client.go @@ -522,7 +522,9 @@ func (c Client) execTable(verb, url string, headers map[string]string, body io.R } func readResponseBody(resp *http.Response) ([]byte, error) { - defer resp.Body.Close() + defer func() { + _ = resp.Body.Close() + }() out, err := ioutil.ReadAll(resp.Body) if err == io.EOF { err = nil diff --git a/storage/file.go b/storage/file.go index 2397587c886d..81501c476a77 100644 --- a/storage/file.go +++ b/storage/file.go @@ -100,7 +100,9 @@ func (f FileServiceClient) ListShares(params ListSharesParameters) (ShareListRes if err != nil { return out, err } - defer resp.body.Close() + defer func() { + _ = resp.body.Close() + }() err = xmlUnmarshal(resp.body, &out) return out, err @@ -115,7 +117,9 @@ func (f FileServiceClient) CreateShare(name string) error { if err != nil { return err } - defer resp.body.Close() + defer func() { + _ = resp.body.Close() + }() return checkRespCode(resp.statusCode, []int{http.StatusCreated}) } @@ -127,7 +131,9 @@ func (f FileServiceClient) ShareExists(name string) (bool, error) { resp, err := f.client.exec("HEAD", uri, headers, nil) if resp != nil { - defer resp.body.Close() + defer func() { + _ = resp.body.Close() + }() if resp.statusCode == http.StatusOK || resp.statusCode == http.StatusNotFound { return resp.statusCode == http.StatusOK, nil } @@ -151,7 +157,9 @@ func (f FileServiceClient) GetShareURL(name string) string { func (f FileServiceClient) CreateShareIfNotExists(name string) (bool, error) { resp, err := f.createShare(name) if resp != nil { - defer resp.body.Close() + defer func() { + _ = resp.body.Close() + }() if resp.statusCode == http.StatusCreated || resp.statusCode == http.StatusConflict { return resp.statusCode == http.StatusCreated, nil } @@ -179,7 +187,9 @@ func (f FileServiceClient) GetShareProperties(name string) (*ShareProperties, er if err != nil { return nil, err } - defer resp.body.Close() + defer func() { + _ = resp.body.Close() + }() if err := checkRespCode(resp.statusCode, []int{http.StatusOK}); err != nil { return nil, err @@ -218,7 +228,9 @@ func (f FileServiceClient) SetShareProperties(name string, shareHeaders ShareHea if err != nil { return err } - defer resp.body.Close() + defer func() { + _ = resp.body.Close() + }() return checkRespCode(resp.statusCode, []int{http.StatusOK}) } @@ -233,7 +245,9 @@ func (f FileServiceClient) DeleteShare(name string) error { if err != nil { return err } - defer resp.body.Close() + defer func() { + _ = resp.body.Close() + }() return checkRespCode(resp.statusCode, []int{http.StatusAccepted}) } @@ -246,7 +260,9 @@ func (f FileServiceClient) DeleteShare(name string) error { func (f FileServiceClient) DeleteShareIfExists(name string) (bool, error) { resp, err := f.deleteShare(name) if resp != nil { - defer resp.body.Close() + defer func() { + _ = resp.body.Close() + }() if resp.statusCode == http.StatusAccepted || resp.statusCode == http.StatusNotFound { return resp.statusCode == http.StatusAccepted, nil } @@ -291,7 +307,9 @@ func (f FileServiceClient) SetShareMetadata(name string, metadata map[string]str if err != nil { return err } - defer resp.body.Close() + defer func() { + _ = resp.body.Close() + }() return checkRespCode(resp.statusCode, []int{http.StatusOK}) } @@ -314,7 +332,9 @@ func (f FileServiceClient) GetShareMetadata(name string) (map[string]string, err if err != nil { return nil, err } - defer resp.body.Close() + defer func() { + _ = resp.body.Close() + }() if err := checkRespCode(resp.statusCode, []int{http.StatusOK}); err != nil { return nil, err diff --git a/storage/queue.go b/storage/queue.go index 3ecf4aca0d21..8311e81819d9 100644 --- a/storage/queue.go +++ b/storage/queue.go @@ -142,7 +142,9 @@ func (c QueueServiceClient) SetMetadata(name string, metadata map[string]string) if err != nil { return err } - defer resp.body.Close() + defer func() { + _ = resp.body.Close() + }() return checkRespCode(resp.statusCode, []int{http.StatusNoContent}) } @@ -165,7 +167,9 @@ func (c QueueServiceClient) GetMetadata(name string) (QueueMetadataResponse, err if err != nil { return qm, err } - defer resp.body.Close() + defer func() { + _ = resp.body.Close() + }() for k, v := range resp.headers { if len(v) != 1 { @@ -198,7 +202,9 @@ func (c QueueServiceClient) CreateQueue(name string) error { if err != nil { return err } - defer resp.body.Close() + defer func() { + _ = resp.body.Close() + }() return checkRespCode(resp.statusCode, []int{http.StatusCreated}) } @@ -211,7 +217,9 @@ func (c QueueServiceClient) DeleteQueue(name string) error { if err != nil { return err } - defer resp.body.Close() + defer func() { + _ = resp.body.Close() + }() return checkRespCode(resp.statusCode, []int{http.StatusNoContent}) } @@ -242,7 +250,9 @@ func (c QueueServiceClient) PutMessage(queue string, message string, params PutM if err != nil { return err } - defer resp.body.Close() + defer func() { + _ = resp.body.Close() + }() return checkRespCode(resp.statusCode, []int{http.StatusCreated}) } @@ -255,7 +265,9 @@ func (c QueueServiceClient) ClearMessages(queue string) error { if err != nil { return err } - defer resp.body.Close() + defer func() { + _ = resp.body.Close() + }() return checkRespCode(resp.statusCode, []int{http.StatusNoContent}) } @@ -270,7 +282,9 @@ func (c QueueServiceClient) GetMessages(queue string, params GetMessagesParamete if err != nil { return r, err } - defer resp.body.Close() + defer func() { + _ = resp.body.Close() + }() err = xmlUnmarshal(resp.body, &r) return r, err } @@ -286,7 +300,9 @@ func (c QueueServiceClient) PeekMessages(queue string, params PeekMessagesParame if err != nil { return r, err } - defer resp.body.Close() + defer func() { + _ = resp.body.Close() + }() err = xmlUnmarshal(resp.body, &r) return r, err } @@ -301,6 +317,8 @@ func (c QueueServiceClient) DeleteMessage(queue, messageID, popReceipt string) e if err != nil { return err } - defer resp.body.Close() + defer func() { + _ = resp.body.Close() + }() return checkRespCode(resp.statusCode, []int{http.StatusNoContent}) } diff --git a/storage/table.go b/storage/table.go index 0826e78672ca..35c5d26ecbce 100644 --- a/storage/table.go +++ b/storage/table.go @@ -49,7 +49,9 @@ func (c *TableServiceClient) QueryTables() ([]AzureTable, error) { if err != nil { return nil, err } - defer resp.body.Close() + defer func() { + _ = resp.body.Close() + }() if err := checkRespCode(resp.statusCode, []int{http.StatusOK}); err != nil { return nil, err @@ -95,7 +97,9 @@ func (c *TableServiceClient) CreateTable(table AzureTable) error { if err != nil { return err } - defer resp.body.Close() + defer func() { + _ = resp.body.Close() + }() if err := checkRespCode(resp.statusCode, []int{http.StatusCreated}); err != nil { return err @@ -121,7 +125,9 @@ func (c *TableServiceClient) DeleteTable(table AzureTable) error { if err != nil { return err } - defer resp.body.Close() + defer func() { + _ = resp.body.Close() + }() if err := checkRespCode(resp.statusCode, []int{http.StatusNoContent}); err != nil { return err diff --git a/storage/table_entities.go b/storage/table_entities.go index 3d4bf501ea03..eae18edd2084 100644 --- a/storage/table_entities.go +++ b/storage/table_entities.go @@ -107,7 +107,9 @@ func (c *TableServiceClient) QueryTableEntities(tableName AzureTable, previousCo if err != nil { return nil, contToken, err } - defer resp.body.Close() + defer func() { + _ = resp.body.Close() + }() if err := checkRespCode(resp.statusCode, []int{http.StatusOK}); err != nil { return nil, contToken, err @@ -156,7 +158,9 @@ func (c *TableServiceClient) execTable(table AzureTable, entity TableEntity, spe return 0, err } - defer resp.body.Close() + defer func() { + _ = resp.body.Close() + }() return resp.statusCode, nil } @@ -214,7 +218,9 @@ func (c *TableServiceClient) DeleteEntity(table AzureTable, entity TableEntity, if err != nil { return err } - defer resp.body.Close() + defer func() { + _ = resp.body.Close() + }() if err := checkRespCode(resp.statusCode, []int{http.StatusNoContent}); err != nil { return err From bf23f32a462732b921287b4faf73a8465230a6c4 Mon Sep 17 00:00:00 2001 From: mcardosos Date: Thu, 8 Dec 2016 16:41:56 -0800 Subject: [PATCH 10/14] Fixed merged stuff --- storage/blob.go | 4 +++- storage/file.go | 27 +++++++++++++++++++-------- storage/queue.go | 4 +++- 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/storage/blob.go b/storage/blob.go index 7e331caebb8c..4cdf1aef7b77 100644 --- a/storage/blob.go +++ b/storage/blob.go @@ -1401,7 +1401,9 @@ func (b BlobStorageClient) AbortBlobCopy(container, name, copyID, currentLeaseID if err != nil { return err } - defer resp.body.Close() + defer func() { + _ = resp.body.Close() + }() if err := checkRespCode(resp.statusCode, []int{http.StatusNoContent}); err != nil { return err diff --git a/storage/file.go b/storage/file.go index a2ead6d65526..3e5ddf06f5ef 100644 --- a/storage/file.go +++ b/storage/file.go @@ -250,8 +250,9 @@ func (f FileServiceClient) ListDirsAndFiles(path string, params ListDirsAndFiles if err != nil { return out, err } - - defer resp.body.Close() + defer func() { + _ = resp.body.Close() + }() err = xmlUnmarshal(resp.body, &out) return out, err } @@ -275,7 +276,9 @@ func (f FileServiceClient) ListFileRanges(path string, listRange *FileRange) (Fi return out, err } - defer resp.body.Close() + defer func() { + _ = resp.body.Close() + }() var cl uint64 cl, err = strconv.ParseUint(resp.headers.Get("x-ms-content-length"), 10, 64) if err != nil { @@ -324,7 +327,7 @@ func (f FileServiceClient) listContent(path string, params url.Values, extraHead } if err = checkRespCode(resp.statusCode, []int{http.StatusOK}); err != nil { - resp.body.Close() + _ = resp.body.Close() return nil, err } @@ -425,7 +428,9 @@ func (f FileServiceClient) GetFile(path string, fileRange *FileRange) (*FileStre } if err = checkRespCode(resp.statusCode, []int{http.StatusOK, http.StatusPartialContent}); err != nil { - resp.body.Close() + defer func() { + _ = resp.body.Close() + }() return nil, err } @@ -501,7 +506,9 @@ func (f FileServiceClient) GetShareURL(name string) string { func (f FileServiceClient) CreateDirectoryIfNotExists(path string) (bool, error) { resp, err := f.createResourceNoClose(path, resourceDirectory, nil) if resp != nil { - defer resp.body.Close() + defer func() { + _ = resp.body.Close() + }() if resp.statusCode == http.StatusCreated || resp.statusCode == http.StatusConflict { return resp.statusCode == http.StatusCreated, nil } @@ -533,7 +540,9 @@ func (f FileServiceClient) createResource(path string, res resourceType, extraHe if err != nil { return err } - defer resp.body.Close() + defer func() { + _ = resp.body.Close() + }() return checkRespCode(resp.statusCode, []int{http.StatusCreated}) } @@ -718,7 +727,9 @@ func (f FileServiceClient) deleteResource(path string, res resourceType) error { if err != nil { return err } - defer resp.body.Close() + defer func() { + _ = resp.body.Close() + }() return checkRespCode(resp.statusCode, []int{http.StatusAccepted}) } diff --git a/storage/queue.go b/storage/queue.go index 1f953e42378d..08e4e1143735 100644 --- a/storage/queue.go +++ b/storage/queue.go @@ -335,7 +335,9 @@ func (c QueueServiceClient) DeleteMessage(queue, messageID, popReceipt string) e if err != nil { return err } - defer resp.body.Close() + defer func() { + _ = resp.body.Close() + }() return checkRespCode(resp.statusCode, []int{http.StatusNoContent}) } From ef160cfee2047b79d307f6ae6c90f87765419176 Mon Sep 17 00:00:00 2001 From: mcardosos Date: Fri, 9 Dec 2016 11:04:42 -0800 Subject: [PATCH 11/14] Fix management errcheck --- management/util.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/management/util.go b/management/util.go index 72601757e550..421fd6566c73 100644 --- a/management/util.go +++ b/management/util.go @@ -6,6 +6,9 @@ import ( ) func getResponseBody(response *http.Response) ([]byte, error) { - defer response.Body.Close() + defer func() { + _ = response.Body.Close() + }() + return ioutil.ReadAll(response.Body) } From f7e4e4224adbb2c3c6d22da81f9b5610fd67d2ff Mon Sep 17 00:00:00 2001 From: mcardosos Date: Fri, 9 Dec 2016 11:20:36 -0800 Subject: [PATCH 12/14] Exclude errors rule in gas scanning --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index db98837d2b84..c7aa700eb049 100644 --- a/.travis.yml +++ b/.travis.yml @@ -15,7 +15,7 @@ install: - glide install script: - - gas -skip=*/arm/*/models.go -skip=*/management/examples/*.go -skip=*vendor* -skip=*/Gododir/* ./... + - gas -exclude=G104 -skip=*/arm/*/models.go -skip=*/management/examples/*.go -skip=*vendor* -skip=*/Gododir/* ./... - test -z "$(gofmt -s -l $(find ./arm/* -type d -print) | tee /dev/stderr)" - test -z "$(gofmt -s -l -w management | tee /dev/stderr)" - test -z "$(gofmt -s -l -w storage | tee /dev/stderr)" From 3fa71a05137c074d64815e63c9dc6972a4a34792 Mon Sep 17 00:00:00 2001 From: mcardosos Date: Fri, 9 Dec 2016 12:10:28 -0800 Subject: [PATCH 13/14] Ignore checkerr issues involving Close() --- .travis.yml | 6 +- management/util.go | 5 +- storage/blob.go | 112 ++++++++++---------------------------- storage/client.go | 4 +- storage/file.go | 52 +++++------------- storage/queue.go | 40 ++++---------- storage/table.go | 12 +--- storage/table_entities.go | 12 +--- 8 files changed, 61 insertions(+), 182 deletions(-) diff --git a/.travis.yml b/.travis.yml index c7aa700eb049..dc76f4b9bbe7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -15,7 +15,7 @@ install: - glide install script: - - gas -exclude=G104 -skip=*/arm/*/models.go -skip=*/management/examples/*.go -skip=*vendor* -skip=*/Gododir/* ./... + - gas -skip=*/arm/*/models.go -skip=*/management/examples/*.go -skip=*vendor* -skip=*/Gododir/* ./... - test -z "$(gofmt -s -l $(find ./arm/* -type d -print) | tee /dev/stderr)" - test -z "$(gofmt -s -l -w management | tee /dev/stderr)" - test -z "$(gofmt -s -l -w storage | tee /dev/stderr)" @@ -31,6 +31,4 @@ script: - go vet ./management/... - test -z "$(golint ./Gododir/... | tee /dev/stderr)" - go vet ./Gododir/... - - set -e; for v in ./arm/... ./datalake-store/... ./Gododir/... ./management/... ./storage/...; do errcheck -ignoretests "$v"; done - - \ No newline at end of file + - set -e; for v in ./arm/... ./datalake-store/... ./Gododir/... ./management/... ./storage/...; do (errcheck -ignoretests "$v" | grep -v 'Close()' | tee /dev/stderr); done diff --git a/management/util.go b/management/util.go index 421fd6566c73..72601757e550 100644 --- a/management/util.go +++ b/management/util.go @@ -6,9 +6,6 @@ import ( ) func getResponseBody(response *http.Response) ([]byte, error) { - defer func() { - _ = response.Body.Close() - }() - + defer response.Body.Close() return ioutil.ReadAll(response.Body) } diff --git a/storage/blob.go b/storage/blob.go index 4cdf1aef7b77..aaf11bb4d031 100644 --- a/storage/blob.go +++ b/storage/blob.go @@ -439,9 +439,7 @@ func (b BlobStorageClient) ListContainers(params ListContainersParameters) (Cont if err != nil { return out, err } - defer func() { - _ = resp.body.Close() - }() + defer resp.body.Close() err = xmlUnmarshal(resp.body, &out) return out, err @@ -456,9 +454,7 @@ func (b BlobStorageClient) CreateContainer(name string, access ContainerAccessTy if err != nil { return err } - defer func() { - _ = resp.body.Close() - }() + defer resp.body.Close() return checkRespCode(resp.statusCode, []int{http.StatusCreated}) } @@ -467,9 +463,7 @@ func (b BlobStorageClient) CreateContainer(name string, access ContainerAccessTy func (b BlobStorageClient) CreateContainerIfNotExists(name string, access ContainerAccessType) (bool, error) { resp, err := b.createContainer(name, access) if resp != nil { - defer func() { - _ = resp.body.Close() - }() + defer resp.body.Close() if resp.statusCode == http.StatusCreated || resp.statusCode == http.StatusConflict { return resp.statusCode == http.StatusCreated, nil } @@ -495,9 +489,7 @@ func (b BlobStorageClient) ContainerExists(name string) (bool, error) { resp, err := b.client.exec(http.MethodHead, uri, headers, nil) if resp != nil { - defer func() { - _ = resp.body.Close() - }() + defer resp.body.Close() if resp.statusCode == http.StatusOK || resp.statusCode == http.StatusNotFound { return resp.statusCode == http.StatusOK, nil } @@ -545,9 +537,7 @@ func (b BlobStorageClient) SetContainerPermissions(container string, containerPe } if resp != nil { - defer func() { - err = resp.body.Close() - }() + defer resp.body.Close() if resp.statusCode != http.StatusOK { return errors.New("Unable to set permissions") @@ -583,9 +573,7 @@ func (b BlobStorageClient) GetContainerPermissions(container string, timeout int // containerAccess. Blob, Container, empty containerAccess := resp.headers.Get(http.CanonicalHeaderKey(ContainerAccessHeader)) - defer func() { - err = resp.body.Close() - }() + defer resp.body.Close() var out AccessPolicy err = xmlUnmarshal(resp.body, &out.SignedIdentifiersList) @@ -609,9 +597,7 @@ func (b BlobStorageClient) DeleteContainer(name string) error { if err != nil { return err } - defer func() { - _ = resp.body.Close() - }() + defer resp.body.Close() return checkRespCode(resp.statusCode, []int{http.StatusAccepted}) } @@ -624,9 +610,7 @@ func (b BlobStorageClient) DeleteContainer(name string) error { func (b BlobStorageClient) DeleteContainerIfExists(name string) (bool, error) { resp, err := b.deleteContainer(name) if resp != nil { - defer func() { - _ = resp.body.Close() - }() + defer resp.body.Close() if resp.statusCode == http.StatusAccepted || resp.statusCode == http.StatusNotFound { return resp.statusCode == http.StatusAccepted, nil } @@ -657,9 +641,7 @@ func (b BlobStorageClient) ListBlobs(container string, params ListBlobsParameter if err != nil { return out, err } - defer func() { - _ = resp.body.Close() - }() + defer resp.body.Close() err = xmlUnmarshal(resp.body, &out) return out, err @@ -672,9 +654,7 @@ func (b BlobStorageClient) BlobExists(container, name string) (bool, error) { headers := b.client.getStandardHeaders() resp, err := b.client.exec(http.MethodHead, uri, headers, nil) if resp != nil { - defer func() { - _ = resp.body.Close() - }() + defer resp.body.Close() if resp.statusCode == http.StatusOK || resp.statusCode == http.StatusNotFound { return resp.statusCode == http.StatusOK, nil } @@ -753,9 +733,7 @@ func (b BlobStorageClient) leaseCommonPut(container string, name string, headers if err != nil { return nil, err } - defer func() { - _ = resp.body.Close() - }() + defer resp.body.Close() if err := checkRespCode(resp.statusCode, []int{expectedStatus}); err != nil { return nil, err @@ -924,9 +902,7 @@ func (b BlobStorageClient) GetBlobProperties(container, name string) (*BlobPrope if err != nil { return nil, err } - defer func() { - _ = resp.body.Close() - }() + defer resp.body.Close() if err = checkRespCode(resp.statusCode, []int{http.StatusOK}); err != nil { return nil, err @@ -995,9 +971,7 @@ func (b BlobStorageClient) SetBlobProperties(container, name string, blobHeaders if err != nil { return err } - defer func() { - _ = resp.body.Close() - }() + defer resp.body.Close() return checkRespCode(resp.statusCode, []int{http.StatusOK}) } @@ -1026,9 +1000,7 @@ func (b BlobStorageClient) SetBlobMetadata(container, name string, metadata map[ if err != nil { return err } - defer func() { - _ = resp.body.Close() - }() + defer resp.body.Close() return checkRespCode(resp.statusCode, []int{http.StatusOK}) } @@ -1048,9 +1020,7 @@ func (b BlobStorageClient) GetBlobMetadata(container, name string) (map[string]s if err != nil { return nil, err } - defer func() { - _ = resp.body.Close() - }() + defer resp.body.Close() if err := checkRespCode(resp.statusCode, []int{http.StatusOK}); err != nil { return nil, err @@ -1109,9 +1079,7 @@ func (b BlobStorageClient) CreateBlockBlobFromReader(container, name string, siz if err != nil { return err } - defer func() { - _ = resp.body.Close() - }() + defer resp.body.Close() return checkRespCode(resp.statusCode, []int{http.StatusCreated}) } @@ -1149,9 +1117,7 @@ func (b BlobStorageClient) PutBlockWithLength(container, name, blockID string, s return err } - defer func() { - _ = resp.body.Close() - }() + defer resp.body.Close() return checkRespCode(resp.statusCode, []int{http.StatusCreated}) } @@ -1169,9 +1135,7 @@ func (b BlobStorageClient) PutBlockList(container, name string, blocks []Block) if err != nil { return err } - defer func() { - _ = resp.body.Close() - }() + defer resp.body.Close() return checkRespCode(resp.statusCode, []int{http.StatusCreated}) } @@ -1188,9 +1152,7 @@ func (b BlobStorageClient) GetBlockList(container, name string, blockType BlockL if err != nil { return out, err } - defer func() { - _ = resp.body.Close() - }() + defer resp.body.Close() err = xmlUnmarshal(resp.body, &out) return out, err @@ -1216,9 +1178,7 @@ func (b BlobStorageClient) PutPageBlob(container, name string, size int64, extra if err != nil { return err } - defer func() { - _ = resp.body.Close() - }() + defer resp.body.Close() return checkRespCode(resp.statusCode, []int{http.StatusCreated}) } @@ -1253,9 +1213,7 @@ func (b BlobStorageClient) PutPage(container, name string, startByte, endByte in if err != nil { return err } - defer func() { - _ = resp.body.Close() - }() + defer resp.body.Close() return checkRespCode(resp.statusCode, []int{http.StatusCreated}) } @@ -1273,9 +1231,7 @@ func (b BlobStorageClient) GetPageRanges(container, name string) (GetPageRangesR if err != nil { return out, err } - defer func() { - _ = resp.body.Close() - }() + defer resp.body.Close() if err = checkRespCode(resp.statusCode, []int{http.StatusOK}); err != nil { return out, err @@ -1302,9 +1258,7 @@ func (b BlobStorageClient) PutAppendBlob(container, name string, extraHeaders ma if err != nil { return err } - defer func() { - _ = resp.body.Close() - }() + defer resp.body.Close() return checkRespCode(resp.statusCode, []int{http.StatusCreated}) } @@ -1327,9 +1281,7 @@ func (b BlobStorageClient) AppendBlock(container, name string, chunk []byte, ext if err != nil { return err } - defer func() { - _ = resp.body.Close() - }() + defer resp.body.Close() return checkRespCode(resp.statusCode, []int{http.StatusCreated}) } @@ -1364,9 +1316,7 @@ func (b BlobStorageClient) StartBlobCopy(container, name, sourceBlob string) (st if err != nil { return "", err } - defer func() { - _ = resp.body.Close() - }() + defer resp.body.Close() if err := checkRespCode(resp.statusCode, []int{http.StatusAccepted, http.StatusCreated}); err != nil { return "", err @@ -1401,9 +1351,7 @@ func (b BlobStorageClient) AbortBlobCopy(container, name, copyID, currentLeaseID if err != nil { return err } - defer func() { - _ = resp.body.Close() - }() + defer resp.body.Close() if err := checkRespCode(resp.statusCode, []int{http.StatusNoContent}); err != nil { return err @@ -1447,9 +1395,7 @@ func (b BlobStorageClient) DeleteBlob(container, name string, extraHeaders map[s if err != nil { return err } - defer func() { - _ = resp.body.Close() - }() + defer resp.body.Close() return checkRespCode(resp.statusCode, []int{http.StatusAccepted}) } @@ -1460,9 +1406,7 @@ func (b BlobStorageClient) DeleteBlob(container, name string, extraHeaders map[s func (b BlobStorageClient) DeleteBlobIfExists(container, name string, extraHeaders map[string]string) (bool, error) { resp, err := b.deleteBlob(container, name, extraHeaders) if resp != nil { - defer func() { - _ = resp.body.Close() - }() + defer resp.body.Close() if resp.statusCode == http.StatusAccepted || resp.statusCode == http.StatusNotFound { return resp.statusCode == http.StatusAccepted, nil } diff --git a/storage/client.go b/storage/client.go index b2430f736331..8f198d7940e0 100644 --- a/storage/client.go +++ b/storage/client.go @@ -521,9 +521,7 @@ func (c Client) execTable(verb, url string, headers map[string]string, body io.R } func readResponseBody(resp *http.Response) ([]byte, error) { - defer func() { - _ = resp.Body.Close() - }() + defer resp.Body.Close() out, err := ioutil.ReadAll(resp.Body) if err == io.EOF { err = nil diff --git a/storage/file.go b/storage/file.go index 3e5ddf06f5ef..497b45d5a5a6 100644 --- a/storage/file.go +++ b/storage/file.go @@ -250,9 +250,7 @@ func (f FileServiceClient) ListDirsAndFiles(path string, params ListDirsAndFiles if err != nil { return out, err } - defer func() { - _ = resp.body.Close() - }() + defer resp.body.Close() err = xmlUnmarshal(resp.body, &out) return out, err } @@ -276,9 +274,7 @@ func (f FileServiceClient) ListFileRanges(path string, listRange *FileRange) (Fi return out, err } - defer func() { - _ = resp.body.Close() - }() + defer resp.body.Close() var cl uint64 cl, err = strconv.ParseUint(resp.headers.Get("x-ms-content-length"), 10, 64) if err != nil { @@ -305,9 +301,7 @@ func (f FileServiceClient) ListShares(params ListSharesParameters) (ShareListRes if err != nil { return out, err } - defer func() { - _ = resp.body.Close() - }() + defer resp.body.Close() err = xmlUnmarshal(resp.body, &out) return out, err } @@ -404,9 +398,7 @@ func (f FileServiceClient) modifyRange(path string, bytes io.Reader, fileRange F if err != nil { return err } - defer func() { - _ = resp.body.Close() - }() + defer resp.body.Close() return checkRespCode(resp.statusCode, []int{http.StatusCreated}) } @@ -428,9 +420,7 @@ func (f FileServiceClient) GetFile(path string, fileRange *FileRange) (*FileStre } if err = checkRespCode(resp.statusCode, []int{http.StatusOK, http.StatusPartialContent}); err != nil { - defer func() { - _ = resp.body.Close() - }() + defer resp.body.Close() return nil, err } @@ -474,9 +464,7 @@ func (f FileServiceClient) resourceExists(path string, res resourceType) (bool, resp, err := f.client.exec(http.MethodHead, uri, headers, nil) if resp != nil { - defer func() { - _ = resp.body.Close() - }() + defer resp.body.Close() if resp.statusCode == http.StatusOK || resp.statusCode == http.StatusNotFound { return resp.statusCode == http.StatusOK, nil } @@ -506,9 +494,7 @@ func (f FileServiceClient) GetShareURL(name string) string { func (f FileServiceClient) CreateDirectoryIfNotExists(path string) (bool, error) { resp, err := f.createResourceNoClose(path, resourceDirectory, nil) if resp != nil { - defer func() { - _ = resp.body.Close() - }() + defer resp.body.Close() if resp.statusCode == http.StatusCreated || resp.statusCode == http.StatusConflict { return resp.statusCode == http.StatusCreated, nil } @@ -524,9 +510,7 @@ func (f FileServiceClient) CreateDirectoryIfNotExists(path string) (bool, error) func (f FileServiceClient) CreateShareIfNotExists(name string) (bool, error) { resp, err := f.createResourceNoClose(ToPathSegment(name), resourceShare, nil) if resp != nil { - defer func() { - _ = resp.body.Close() - }() + defer resp.body.Close() if resp.statusCode == http.StatusCreated || resp.statusCode == http.StatusConflict { return resp.statusCode == http.StatusCreated, nil } @@ -540,9 +524,7 @@ func (f FileServiceClient) createResource(path string, res resourceType, extraHe if err != nil { return err } - defer func() { - _ = resp.body.Close() - }() + defer resp.body.Close() return checkRespCode(resp.statusCode, []int{http.StatusCreated}) } @@ -631,9 +613,7 @@ func (f FileServiceClient) getResourceHeaders(path string, comp compType, res re if err != nil { return nil, err } - defer func() { - _ = resp.body.Close() - }() + defer resp.body.Close() if err = checkRespCode(resp.statusCode, []int{http.StatusOK}); err != nil { return nil, err @@ -711,9 +691,7 @@ func (f FileServiceClient) DeleteShare(name string) error { func (f FileServiceClient) DeleteShareIfExists(name string) (bool, error) { resp, err := f.deleteResourceNoClose(ToPathSegment(name), resourceShare) if resp != nil { - defer func() { - _ = resp.body.Close() - }() + defer resp.body.Close() if resp.statusCode == http.StatusAccepted || resp.statusCode == http.StatusNotFound { return resp.statusCode == http.StatusAccepted, nil } @@ -727,9 +705,7 @@ func (f FileServiceClient) deleteResource(path string, res resourceType) error { if err != nil { return err } - defer func() { - _ = resp.body.Close() - }() + defer resp.body.Close() return checkRespCode(resp.statusCode, []int{http.StatusAccepted}) } @@ -816,9 +792,7 @@ func (f FileServiceClient) setResourceHeaders(path string, comp compType, res re if err != nil { return err } - defer func() { - _ = resp.body.Close() - }() + defer resp.body.Close() return checkRespCode(resp.statusCode, []int{http.StatusOK}) } diff --git a/storage/queue.go b/storage/queue.go index 08e4e1143735..b33dc5011013 100644 --- a/storage/queue.go +++ b/storage/queue.go @@ -160,9 +160,7 @@ func (c QueueServiceClient) SetMetadata(name string, metadata map[string]string) if err != nil { return err } - defer func() { - _ = resp.body.Close() - }() + defer resp.body.Close() return checkRespCode(resp.statusCode, []int{http.StatusNoContent}) } @@ -185,9 +183,7 @@ func (c QueueServiceClient) GetMetadata(name string) (QueueMetadataResponse, err if err != nil { return qm, err } - defer func() { - _ = resp.body.Close() - }() + defer resp.body.Close() for k, v := range resp.headers { if len(v) != 1 { @@ -220,9 +216,7 @@ func (c QueueServiceClient) CreateQueue(name string) error { if err != nil { return err } - defer func() { - _ = resp.body.Close() - }() + defer resp.body.Close() return checkRespCode(resp.statusCode, []int{http.StatusCreated}) } @@ -235,9 +229,7 @@ func (c QueueServiceClient) DeleteQueue(name string) error { if err != nil { return err } - defer func() { - _ = resp.body.Close() - }() + defer resp.body.Close() return checkRespCode(resp.statusCode, []int{http.StatusNoContent}) } @@ -268,9 +260,7 @@ func (c QueueServiceClient) PutMessage(queue string, message string, params PutM if err != nil { return err } - defer func() { - _ = resp.body.Close() - }() + defer resp.body.Close() return checkRespCode(resp.statusCode, []int{http.StatusCreated}) } @@ -283,9 +273,7 @@ func (c QueueServiceClient) ClearMessages(queue string) error { if err != nil { return err } - defer func() { - _ = resp.body.Close() - }() + defer resp.body.Close() return checkRespCode(resp.statusCode, []int{http.StatusNoContent}) } @@ -300,9 +288,7 @@ func (c QueueServiceClient) GetMessages(queue string, params GetMessagesParamete if err != nil { return r, err } - defer func() { - _ = resp.body.Close() - }() + defer resp.body.Close() err = xmlUnmarshal(resp.body, &r) return r, err } @@ -318,9 +304,7 @@ func (c QueueServiceClient) PeekMessages(queue string, params PeekMessagesParame if err != nil { return r, err } - defer func() { - _ = resp.body.Close() - }() + defer resp.body.Close() err = xmlUnmarshal(resp.body, &r) return r, err } @@ -335,9 +319,7 @@ func (c QueueServiceClient) DeleteMessage(queue, messageID, popReceipt string) e if err != nil { return err } - defer func() { - _ = resp.body.Close() - }() + deferresp.body.Close() return checkRespCode(resp.statusCode, []int{http.StatusNoContent}) } @@ -357,8 +339,6 @@ func (c QueueServiceClient) UpdateMessage(queue string, messageID string, messag if err != nil { return err } - defer func() { - _ = resp.body.Close() - }() + defer resp.body.Close() return checkRespCode(resp.statusCode, []int{http.StatusNoContent}) } diff --git a/storage/table.go b/storage/table.go index f07ebdf85f9c..ef5fdff1da58 100644 --- a/storage/table.go +++ b/storage/table.go @@ -49,9 +49,7 @@ func (c *TableServiceClient) QueryTables() ([]AzureTable, error) { if err != nil { return nil, err } - defer func() { - _ = resp.body.Close() - }() + defer resp.body.Close() if err := checkRespCode(resp.statusCode, []int{http.StatusOK}); err != nil { return nil, err @@ -97,9 +95,7 @@ func (c *TableServiceClient) CreateTable(table AzureTable) error { if err != nil { return err } - defer func() { - _ = resp.body.Close() - }() + defer resp.body.Close() if err := checkRespCode(resp.statusCode, []int{http.StatusCreated}); err != nil { return err @@ -125,9 +121,7 @@ func (c *TableServiceClient) DeleteTable(table AzureTable) error { if err != nil { return err } - defer func() { - _ = resp.body.Close() - }() + defer resp.body.Close() if err := checkRespCode(resp.statusCode, []int{http.StatusNoContent}); err != nil { return err diff --git a/storage/table_entities.go b/storage/table_entities.go index 7cd69c2d7605..db991407981f 100644 --- a/storage/table_entities.go +++ b/storage/table_entities.go @@ -106,9 +106,7 @@ func (c *TableServiceClient) QueryTableEntities(tableName AzureTable, previousCo contToken := extractContinuationTokenFromHeaders(resp.headers) - defer func() { - _ = resp.body.Close() - }() + defer resp.body.Close() if err = checkRespCode(resp.statusCode, []int{http.StatusOK}); err != nil { return nil, contToken, err @@ -155,9 +153,7 @@ func (c *TableServiceClient) execTable(table AzureTable, entity TableEntity, spe return 0, err } - defer func() { - _ = resp.body.Close() - }() + defer resp.body.Close() return resp.statusCode, nil } @@ -211,9 +207,7 @@ func (c *TableServiceClient) DeleteEntity(table AzureTable, entity TableEntity, if err != nil { return err } - defer func() { - _ = resp.body.Close() - }() + defer resp.body.Close() if err := checkRespCode(resp.statusCode, []int{http.StatusNoContent}); err != nil { return err From 3b3aa5ec7eaecd57b2e4de4c07128828e8b9a4e8 Mon Sep 17 00:00:00 2001 From: mcardosos Date: Mon, 12 Dec 2016 11:42:29 -0800 Subject: [PATCH 14/14] Get rid of gocheck, and add specific gas tests --- .travis.yml | 4 ++-- storage/blob.go | 5 +---- storage/client.go | 16 +++++----------- storage/client_test.go | 4 +--- storage/file.go | 4 ++-- storage/queue.go | 2 +- storage/util.go | 8 +++----- 7 files changed, 15 insertions(+), 28 deletions(-) diff --git a/.travis.yml b/.travis.yml index dc76f4b9bbe7..b3729bb17f7a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,12 +10,13 @@ install: - go get -u golang.org/x/net/context - go get -u github.com/HewlettPackard/gas - go get -u gopkg.in/godo.v2/cmd/godo - - go get -u github.com/kisielk/errcheck - export GO15VENDOREXPERIMENT=1 - glide install script: - gas -skip=*/arm/*/models.go -skip=*/management/examples/*.go -skip=*vendor* -skip=*/Gododir/* ./... + - gas -exclude=G101 ./arm/... ./management/examples/... + - gas -exclude=G204 ./Gododir/... - test -z "$(gofmt -s -l $(find ./arm/* -type d -print) | tee /dev/stderr)" - test -z "$(gofmt -s -l -w management | tee /dev/stderr)" - test -z "$(gofmt -s -l -w storage | tee /dev/stderr)" @@ -31,4 +32,3 @@ script: - go vet ./management/... - test -z "$(golint ./Gododir/... | tee /dev/stderr)" - go vet ./Gododir/... - - set -e; for v in ./arm/... ./datalake-store/... ./Gododir/... ./management/... ./storage/...; do (errcheck -ignoretests "$v" | grep -v 'Close()' | tee /dev/stderr); done diff --git a/storage/blob.go b/storage/blob.go index aaf11bb4d031..d9e707c816ce 100644 --- a/storage/blob.go +++ b/storage/blob.go @@ -1475,10 +1475,7 @@ func (b BlobStorageClient) GetBlobSASURIWithSignedIPAndProtocol(container, name return "", err } - sig, err := b.client.computeHmac256(stringToSign) - if err != nil { - return "", err - } + sig := b.client.computeHmac256(stringToSign) sasParams := url.Values{ "sv": {b.client.apiVersion}, "se": {signedExpiry}, diff --git a/storage/client.go b/storage/client.go index 8f198d7940e0..c75add8744e7 100644 --- a/storage/client.go +++ b/storage/client.go @@ -234,12 +234,9 @@ func (c Client) GetFileService() FileServiceClient { return FileServiceClient{c} } -func (c Client) createAuthorizationHeader(canonicalizedString string) (string, error) { - signature, err := c.computeHmac256(canonicalizedString) - if err != nil { - return "", err - } - return fmt.Sprintf("%s %s:%s", "SharedKey", c.getCanonicalizedAccountName(), signature), nil +func (c Client) createAuthorizationHeader(canonicalizedString string) string { + signature := c.computeHmac256(canonicalizedString) + return fmt.Sprintf("%s %s:%s", "SharedKey", c.getCanonicalizedAccountName(), signature) } func (c Client) getAuthorizationHeader(verb, url string, headers map[string]string) (string, error) { @@ -249,7 +246,7 @@ func (c Client) getAuthorizationHeader(verb, url string, headers map[string]stri } canonicalizedString := c.buildCanonicalizedString(verb, headers, canonicalizedResource) - return c.createAuthorizationHeader(canonicalizedString) + return c.createAuthorizationHeader(canonicalizedString), nil } func (c Client) getStandardHeaders() map[string]string { @@ -503,10 +500,7 @@ func (c Client) createSharedKeyLite(url string, headers map[string]string) (stri } strToSign := headers["x-ms-date"] + "\n" + can - hmac, err := c.computeHmac256(strToSign) - if err != nil { - return "", err - } + hmac := c.computeHmac256(strToSign) return fmt.Sprintf("SharedKeyLite %s:%s", c.accountName, hmac), nil } diff --git a/storage/client_test.go b/storage/client_test.go index 2fd1e4bb0e77..aa1c98243eed 100644 --- a/storage/client_test.go +++ b/storage/client_test.go @@ -244,7 +244,5 @@ func (s *StorageClientSuite) Test_createAuthorizationHeader(c *chk.C) { canonicalizedString := `foobarzoo` expected := `SharedKey foo:h5U0ATVX6SpbFX1H6GNuxIMeXXCILLoIvhflPtuQZ30=` - ah, err := cli.createAuthorizationHeader(canonicalizedString) - c.Assert(err, chk.IsNil) - c.Assert(ah, chk.Equals, expected) + c.Assert(cli.createAuthorizationHeader(canonicalizedString), chk.Equals, expected) } diff --git a/storage/file.go b/storage/file.go index 497b45d5a5a6..716d8c938d24 100644 --- a/storage/file.go +++ b/storage/file.go @@ -321,7 +321,7 @@ func (f FileServiceClient) listContent(path string, params url.Values, extraHead } if err = checkRespCode(resp.statusCode, []int{http.StatusOK}); err != nil { - _ = resp.body.Close() + resp.body.Close() return nil, err } @@ -420,7 +420,7 @@ func (f FileServiceClient) GetFile(path string, fileRange *FileRange) (*FileStre } if err = checkRespCode(resp.statusCode, []int{http.StatusOK, http.StatusPartialContent}); err != nil { - defer resp.body.Close() + resp.body.Close() return nil, err } diff --git a/storage/queue.go b/storage/queue.go index b33dc5011013..ad6c7b0dc6d0 100644 --- a/storage/queue.go +++ b/storage/queue.go @@ -319,7 +319,7 @@ func (c QueueServiceClient) DeleteMessage(queue, messageID, popReceipt string) e if err != nil { return err } - deferresp.body.Close() + defer resp.body.Close() return checkRespCode(resp.statusCode, []int{http.StatusNoContent}) } diff --git a/storage/util.go b/storage/util.go index ba7b62214e27..57ca1b6d937e 100644 --- a/storage/util.go +++ b/storage/util.go @@ -15,12 +15,10 @@ import ( "time" ) -func (c Client) computeHmac256(message string) (string, error) { +func (c Client) computeHmac256(message string) string { h := hmac.New(sha256.New, c.accountKey) - if _, err := h.Write([]byte(message)); err != nil { - return "", fmt.Errorf("Failed computing to hmac256 : %v", err) - } - return base64.StdEncoding.EncodeToString(h.Sum(nil)), nil + h.Write([]byte(message)) + return base64.StdEncoding.EncodeToString(h.Sum(nil)) } func currentTimeRfc1123Formatted() string {