digigo/order.go

82 строки
2.7 KiB
Go
Исходник Постоянная ссылка Обычный вид История

2017-05-22 22:45:27 +03:00
package digigo // import "go.mozilla.org/digigo"
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"github.com/pkg/errors"
)
// Order is the structure used to submit a certificate order to Digicert.
// https://www.digicert.com/services/v2/documentation/order/overview-submit
type Order struct {
Certificate struct {
CommonName string `json:"common_name"`
2017-05-23 04:24:19 +03:00
DNSNames []string `json:"dns_names"`
2017-05-22 22:45:27 +03:00
Csr string `json:"csr"`
2017-05-23 04:24:19 +03:00
OrganizationUnits []string `json:"organization_units,omitempty"`
2017-05-22 22:45:27 +03:00
ServerPlatform struct {
ID int `json:"id"`
2017-05-23 04:24:19 +03:00
} `json:"server_platform,omitempty"`
2017-05-22 22:45:27 +03:00
SignatureHash string `json:"signature_hash"`
2017-05-23 04:24:19 +03:00
ProfileOption string `json:"profile_option,omitempty"`
2017-05-22 22:45:27 +03:00
} `json:"certificate"`
Organization struct {
ID int `json:"id"`
} `json:"organization"`
ValidityYears int `json:"validity_years"`
2017-05-23 04:24:19 +03:00
CustomExpirationDate string `json:"custom_expiration_date,omitempty"`
Comments string `json:"comments,omitempty"`
DisableRenewalNotifications bool `json:"disable_renewal_notifications,omitempty"`
RenewalOfOrderID int `json:"renewal_of_order_id,omitempty"`
PaymentMethod string `json:"payment_method,omitempty"`
2017-05-22 22:45:27 +03:00
}
type orderResponse struct {
ID int `json:"id"`
Requests []struct {
ID int `json:"id"`
Status string `json:"status"`
} `json:"requests"`
}
// SubmitOrder sends an order for a given product to the Digicert API. The productNameId
// determines which product is being ordered. The full list of productNameId an account has
// access to can be obtained via ViewProductList().
//
// This function returns the orderId, or -1 and an error if not successful.
func (cli Client) SubmitOrder(order Order, productNameID string) (int, error) {
orderBody, err := json.Marshal(order)
if err != nil {
return -1, errors.Wrap(err, "failed to marshal order")
}
r, err := http.NewRequest("POST",
cli.baseurl+"/order/certificate/"+productNameID,
bytes.NewBuffer(orderBody))
if err != nil {
return -1, errors.Wrap(err, "failed to prepare order request")
}
resp, err := cli.Do(r)
if err != nil {
return -1, errors.Wrap(err, "failed to submit request to digicert api")
}
if resp.StatusCode != http.StatusCreated {
2017-05-23 04:24:19 +03:00
return -1, errors.Errorf("failed to create order: %s", resp.Status)
2017-05-22 22:45:27 +03:00
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return -1, errors.Wrap(err, "failed to read response body")
}
defer resp.Body.Close()
var or orderResponse
err = json.Unmarshal(body, &or)
if err != nil {
return -1, errors.Wrap(err, "failed to parse JSON response body")
}
if len(or.Requests) != 1 {
return -1, errors.New("no request id was found in digicert's response")
}
return or.Requests[0].ID, nil
}