func New(testing bool) *Domain { // Load the gandi config file, which contains the api keys cfg := config.Load("") var c *client.Client if testing { c = client.New(cfg.ApiTestKey, client.Testing) } else { c = client.New(cfg.ApiProdKey, client.Production) } return &Domain{ domain: domain.New(c), zone: zone.New(c), record: record.New(c), version: version.New(c), } }
func (g *GandiProvider) Init(rootDomainName string) error { var apiKey string if apiKey = os.Getenv("GANDI_APIKEY"); len(apiKey) == 0 { return fmt.Errorf("GANDI_APIKEY is not set") } systemType := gandiClient.Production testing := os.Getenv("GANDI_TESTING") if len(testing) != 0 { logrus.Infof("GANDI_TESTING is set, using testing platform") systemType = gandiClient.Testing } client := gandiClient.New(apiKey, systemType) g.record = gandiRecord.New(client) g.zoneVersion = gandiZoneVersion.New(client) g.operation = gandiOperation.New(client) root := utils.UnFqdn(rootDomainName) split_root := strings.Split(root, ".") split_zoneDomain := split_root[len(split_root)-2 : len(split_root)] zoneDomain := strings.Join(split_zoneDomain, ".") domain := gandiDomain.New(client) domainInfo, err := domain.Info(zoneDomain) if err != nil { return fmt.Errorf("Failed to get zone ID for domain %s: %v", zoneDomain, err) } zoneId := domainInfo.ZoneId zone := gandiZone.New(client) zones, err := zone.List() if err != nil { return fmt.Errorf("Failed to list hosted zones: %v", err) } found := false for _, z := range zones { if z.Id == zoneId { g.root = root g.zone = z found = true break } } if !found { return fmt.Errorf("Zone for '%s' not found", root) } g.zoneDomain = zoneDomain g.zoneSuffix = fmt.Sprintf(".%s", zoneDomain) g.sub = strings.TrimSuffix(root, zoneDomain) g.zoneHandler = zone logrus.Infof("Configured %s for domain '%s' using zone '%s'", g.GetName(), root, g.zone.Name) return nil }
func NewGandiClient(configPath string, testing bool) *client.Client { // Load config cfg := config.Load(configPath) var apiKey string var systemType client.SystemType // Use test system and api key if the Testing flag was provided if testing { apiKey = cfg.ApiTestKey systemType = client.Testing } else { apiKey = cfg.ApiProdKey systemType = client.Production } // Create gandi client return client.New(apiKey, systemType) }
func init() { gandiHandler := &GandiHandler{} apiKey := os.Getenv("GANDI_APIKEY") if len(apiKey) == 0 { logrus.Infof("GANDI_APIKEY is not set, skipping init of %s provider", gandiHandler.GetName()) return } if err := RegisterProvider("gandi", gandiHandler); err != nil { logrus.Fatal("Could not register Gandi provider") } systemType := gandi.Production testing := os.Getenv("GANDI_TESTING") if len(testing) != 0 { logrus.Infof("GANDI_TESTING is set, using testing platform") systemType = gandi.Testing } client := gandi.New(apiKey, systemType) gandiHandler.record = gandiRecord.New(client) gandiHandler.zoneVersion = gandiZoneVersion.New(client) root := strings.TrimSuffix(dns.RootDomainName, ".") split_root := strings.Split(root, ".") split_zoneDomain := split_root[len(split_root)-2 : len(split_root)] zoneDomain := strings.Join(split_zoneDomain, ".") domain := gandiDomain.New(client) domainInfo, err := domain.Info(zoneDomain) if err != nil { logrus.Fatalf("Failed to get zone ID for domain %s: %v", zoneDomain, err) } zoneId := domainInfo.ZoneId zone := gandiZone.New(client) zones, err := zone.List() if err != nil { logrus.Fatalf("Failed to list hosted zones: %v", err) } found := false for _, z := range zones { if z.Id == zoneId { gandiHandler.root = root gandiHandler.zone = z found = true break } } if !found { logrus.Fatalf("Hosted zone %s is missing", root) } gandiHandler.zoneDomain = zoneDomain gandiHandler.zoneSuffix = fmt.Sprintf(".%s", zoneDomain) gandiHandler.sub = strings.TrimSuffix(root, zoneDomain) gandiHandler.zoneHandler = zone logrus.Infof("Configured %s for domain %s using hosted zone %q ", gandiHandler.GetName(), root, gandiHandler.zone.Name) }
func main() { flag.Parse() log.Println("gogogandidns - Gandi Dynamic DNS updater by Diego Elio Pettenò <*****@*****.**>") if *apiFilePath == "" { log.Fatalf("Missing value for -api_file") } if *ifaceName == "" { log.Fatalf("Missing value for -iface") } family, err := godnsupdater.FamilyFromString(*addrFamily) if err != nil { log.Fatalf("Invalid value for -family: %v", err) } if *zoneId == 0 { log.Fatalf("Missing value for -zone") } if *recordName == "" { log.Fatalf("Missing value for -record") } api, err := getApiKey(*apiFilePath) if err != nil { log.Fatal(err) } address, err := godnsupdater.GetInterfaceIP(*ifaceName, family) if err != nil { log.Fatal(err) } env := client.Production if *useTestingEnvironment { env = client.Testing } c := client.New(api, env) newVersion, err := cloneLatestZone(c, *zoneId) if err != nil { log.Fatal(err) } log.Printf("New version %v created for zone %v", newVersion, *zoneId) recordType := godnsupdater.DnsTypeByFamily[family] // Get the list of current entries, filter on the name and type of record, as delete all the records of the same type already present. r := record.New(c) rInfos, err := r.List(*zoneId, newVersion) if err != nil { log.Fatal(err) } for _, rInfo := range rInfos { if rInfo.Name == *recordName && rInfo.Type == recordType { log.Printf("Removing record \"%v\" with ID %v", rInfo.Name, rInfo.Id) ok, err := r.Delete(*zoneId, newVersion, rInfo.Id) if err != nil { log.Fatal(err) } if !ok { log.Fatalf("Deleting record failed, but no error returned.") } } } addArgs := record.RecordAdd{ Zone: *zoneId, Version: newVersion, Name: *recordName, Type: recordType, Value: address, Ttl: 300, } rInfo, err := r.Add(addArgs) if err != nil { log.Fatal(err) } log.Printf("New record \"%v\" created with ID %v", rInfo.Name, rInfo.Id) zv := version.New(c) ok, err := zv.Set(*zoneId, newVersion) if err != nil { log.Fatal(err) } if !ok { log.Fatalf("Setting new version live failed, but no error returned.") } log.Printf("Version %v set live for zone %v", newVersion, *zoneId) }