Showing posts with label ibm. Show all posts
Showing posts with label ibm. Show all posts

2023-04-18

How to use DNS SDK in Golang

So we're gonna try to manipulate DNS records using go SDK (not REST API directly). I went through first 2 page of google search results, and companies that providing SDK for Go were:

  1. IBM networking-go-sdk - 161.26.0.10 and 161.26.0.11 - timedout resolving their own website
  2. AWS route53 - 169.254.169.253 - timedout resolving their own website
  3. DNSimple dnsimple-go - 162.159.27.4 and 199.247.155.53 - 160-180ms and 70-75ms from SG
  4. Google googleapis - 8.8.8.8 and 8.8.4.4 - 0ms for both from SG
  5. GCore gcore-dns-sdk-go - 199.247.155.53 and 2.56.220.2 - 0ms and 0-171ms (171ms on first hit only, the rest is 0ms) from SG

I've used google SDK before for non-DNS stuff, a bit too raw and so many required steps. You have to create a project, enable API, create service account, set permission for that account, download credentials.json, then hit using their SDK -- not really straightforward, so today we're gonna try G-Core's DNS, apparently it's very easy, just need to visit their website and sign up, profile > API Tokens > Create Token, copy it to some file (for example: .token file).

This is example how you can create a zone, add an A record, and delete everything:

 package main

import (
  "context"
  _ "embed"
  "strings"
  "time"

  "github.com/G-Core/gcore-dns-sdk-go"
  "github.com/kokizzu/gotro/L"
)

//go:embed .token
var apiToken string

func main() {
  apiToken = strings.TrimSpace(apiToken)

  // init SDK
  sdk := dnssdk.NewClient(dnssdk.PermanentAPIKeyAuth(apiToken), func(client *dnssdk.Client) {
    client.Debug = true
  })
  ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
  defer cancel()

  const zoneName = `benalu2.dev`

  // create zone
  _, err := sdk.CreateZone(ctx, zoneName)
  if err != nil && !strings.Contains(err.Error(), `already exists`) {
    L.PanicIf(err, `sdk.CreateZone`)
  }

  // get zone
  zoneResp, err := sdk.Zone(ctx, zoneName)
  L.PanicIf(err, `sdk.Zone`)
  L.Describe(zoneResp)

  // add A record
  err = sdk.AddZoneRRSet(ctx,
    zoneName,        // zone
    `www.`+zoneName, // name
    `A`,             // rrtype
    []dnssdk.ResourceRecord{
      {
// https://apidocs.gcore.com/dns#tag/rrsets/operation/CreateRRSet
        Content: []any{
          `194.233.65.174`,
        },
      },
    },
    120, // TTL
  )
  L.PanicIf(err, `AddZoneRRSet`)

  // get A record
  rr, err := sdk.RRSet(ctx, zoneName, `www.`+zoneName, `A`)
  L.PanicIf(err, `sdk.RRSet`)
  L.Describe(rr)

  // delete A record
  err = sdk.DeleteRRSet(ctx, zoneName, `www.`+zoneName, `A`)
  L.PanicIf(err, `sdk.DeleteRRSet`)

  // delete zone
  err = sdk.DeleteZone(ctx, zoneName)
  L.PanicIf(err, `sdk.DeleteZone`)
}

The full source code repo is here. Apparently it's very easy to manipulate DNS record using their SDK, after adding record programmatically, all I need to do is just delegate (set authoritative nameserver) to their NS: ns1.gcorelabs.net and ns2.gcdn.services.

In my case because I bought the domain name on google domains, then I just need to change this: 

 
Then just wait it to be delegated properly (until all DNS servers that still caching the old authorized NS cleared up), I guess that it.

2021-12-28

Object Storage Service with CDN

There's a lot of S3-like service, but some of them doesn't have CDN-like feature, we have to manually cache them or use CDN manually. Today we're gonna compare each service either S3 or CDN storage in terms of storage, bandwidth, and minimum price and location (SG or Tokyo if possible).

These price collected as per 2021-12-28 10:58 GMT+7

Provider Name Location Storage Price/GB/month Bandwidth Price/GB Other Price
Azure Blob SEA $0.195-$0.016 (hot/cold) $0.09 Put cost $0.0296-$0.13, read also have cost
IBM Object Storage Tokyo $0.0237-$0.0085 (auto hot/cold) $0.14 (50TB), $0.11 (+100TB), $0.08 (+350TB) Put cost $0.0050 per 10K
Google Cloud Storage SG $0.020-$0.005 (hot/cold)/GB $0.12 (1TB), $0.11 (9TB), $0.08 Put cost $0.05-$0.004 per 10K
AWS S3 SG $0.025 (50TB), $0.024 (450TB), $0.023 $0.12 (10TB), $0.085 (40TB), $0.082, $0.09 to S3 other S3 region Put cost $0.005 per 1K
Dreamhost Cloud Storage ? $0.025, $0.0238-$0.0146 (40GB, 20TB prepaid) $0.05
BunnyNet CDN+EdgeStorage Asia $0.03 $0.03
Linode Object Storage SG $0.02 $0.01 $5 minimum for 250GB storage/500GB transfer
Vultr Object Storage Not stated, SG available $0.02 $0.01 $5 minimum for 250GB storage/1TB transfer
DigitalOcean Spaces SG $0.02 $0.01 $5 minimum for 250GB storage/1TB transfer
5Cents CDN/Akamai WorldWide $0.05, $0.0143 (alacarte, pay-as-you-go) $0.0075-$0.015 (akamai) $7.5 minimum for 1TB for alacarte
PushR CDN+SFS US, EU, Asia
$0.015 $0.01-$0.04 (depends on network zone)
Backblaze B2 Only US or NL $0.005 $0.01, free if through CDN partner Put cost $0.004 per 10K, 2500 free per day
Contabo EU only
$0.00996 (promo) free $2.49 minimum for 250GB storage
Filebase ? $0.0059 $0.0059 $5.99 minimum for 1TB storage/1TB transfer
Wasabi APAC $0.0068 free*
$6.99 minimum for 1TB storage (=max egress)       
StorJ ? $0.004 ($4/TB after 150GB) $0.007 ($7/TB after 150GB)

What if you need to transfer between S3-compatible provider? try https://packetfabric.com/transporter