Ejemplo n.º 1
0
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
}
Ejemplo n.º 2
0
func cloneLatestZone(c *client.Client, zId int64) (int64, error) {
	z := zone.New(c)
	zInfo, err := z.Info(zId)
	if err != nil {
		return 0, nil
	}

	zv := version.New(c)
	newVersion, err := zv.New(zId, zInfo.Version)
	if err != nil {
		return 0, nil
	}

	return newVersion, nil
}
Ejemplo n.º 3
0
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),
	}
}
Ejemplo n.º 4
0
func main() {
	opts := &Options{}
	goptions.ParseAndFail(opts)

	// Print version number and exit if the version flag is set
	if opts.Version {
		fmt.Printf("gandi-domain-zone-version v%s\n", shared.VersionNumber)
		return
	}

	// Get gandi client
	client := shared.NewGandiClient(opts.ConfigPath, opts.Testing)

	// Create api and zone instances
	api := api.New(client)
	version := cli.New(api)

	switch opts.Verbs {
	case "count":
		version.Count(opts.Count.Zone)

	case "list":
		version.List(opts.List.Zone)

	case "new":
		version.New(opts.New.Zone, opts.New.Version)

	case "delete":
		version.Delete(opts.Delete.Zone, opts.Delete.Version)

	case "set":
		version.Set(opts.Set.Zone, opts.Set.Version)

	default:
		goptions.PrintHelp()
	}
}
Ejemplo n.º 5
0
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)
}
Ejemplo n.º 6
0
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)
}