def attach
      unless addon_name = args.shift
        error("Usage: heroku addons:attach ADDON_NAME\nMust specify add-on resource to attach.")
      end
      addon = resolve_addon!(addon_name)

      requires_preauth

      attachment_name = options[:as]

      msg = attachment_name ?
        "Attaching #{addon['name']} as #{attachment_name} to #{app}" :
        "Attaching #{addon['name']} to #{app}"

      display("#{msg}... ", false)

      response = api.request(
        :body     => json_encode({
          "app"     => {"name" => app},
          "addon"   => {"name" => addon['name']},
          "confirm" => options[:confirm],
          "name"    => attachment_name
        }),
        :expects  => [201, 422],
        :headers  => { "Accept" => "application/vnd.heroku+json; version=3" },
        :method   => :post,
        :path     => "/addon-attachments"
      )

      case response.status
      when 201
        display("done")
        action("Setting #{response.body["name"]} vars and restarting #{app}") do
          @status = api.get_release(app, 'current').body['name']
        end
      when 422 # add-on resource not found or cannot be attached
        display("failed")
        output_with_bang(response.body["message"])
        output_with_bang("List available resources with `heroku addons`.")
        output_with_bang("Provision a new add-on resource with `heroku addons:create ADDON_PLAN`.")
      end
    end
def attach
      unless addon_name = args.shift
        error("Usage: heroku addons:attach ADDON_NAME\nMust specify add-on resource to attach.")
      end
      addon = resolve_addon!(addon_name)

      requires_preauth

      attachment_name = options[:as]

      msg = attachment_name ?
        "Attaching #{addon['name']} as #{attachment_name} to #{app}" :
        "Attaching #{addon['name']} to #{app}"

      display("#{msg}... ", false)

      response = api.request(
        :body     => json_encode({
          "app"     => {"name" => app},
          "addon"   => {"name" => addon['name']},
          "confirm" => options[:confirm],
          "name"    => attachment_name
        }),
        :expects  => [201, 422],
        :headers  => { "Accept" => "application/vnd.heroku+json; version=3" },
        :method   => :post,
        :path     => "/addon-attachments"
      )

      case response.status
      when 201
        display("done")
        action("Setting #{response.body["name"]} vars and restarting #{app}") do
          @status = api.get_release(app, 'current').body['name']
        end
      when 422 # add-on resource not found or cannot be attached
        display("failed")
        output_with_bang(response.body["message"])
        output_with_bang("List available resources with `heroku addons`.")
        output_with_bang("Provision a new add-on resource with `heroku addons:create ADDON_PLAN`.")
      end
    end
addons:attach ADDON_NAME

 attach add-on resource to an app

 --as ATTACHMENT_NAME  # (optional) name for add-on attachment
 --confirm APP_NAME    # overwrite existing add-on attachment with same name