Difference between revisions of "Linode"

From Organic Design wiki
(example in perl)
m
Line 9: Line 9:
  
  
Example Perl code using LWP to update an A record:
+
Example Perl code using LWP to update an A record $HOST.$DOMAIN to $IP:
 
<source lang="perl">
 
<source lang="perl">
 
# Set up a user agent with our token as default header
 
# Set up a user agent with our token as default header
Line 15: Line 15:
 
$ua->default_header(
 
$ua->default_header(
 
'Content-Type'  => "application/json",
 
'Content-Type'  => "application/json",
'Authorization' => "Bearer $dnsApiKey"
+
'Authorization' => "Bearer $API_KEY"
 
);
 
);
  
# Find the ID of the domain
+
# Find the ID $DOMAIN
$json = $ua->get( 'https://api.linode.com/v4/domains' )->content;
+
$domains = $ua->get( 'https://api.linode.com/v4/domains' )->content;
for ( @{ decode_json( $json )->{data} } ) {
+
for ( @{ decode_json( $domains )->{data} } ) {
$domain_id = $_->{id} if $_->{domain} eq $domain;
+
$domain_id = $_->{id} if $_->{domain} eq $DOMAIN;
 
}
 
}
die "Domain name not found" unless $domain_id;
 
  
# Find the ID of the A record having the host name
+
# Find the ID of the A record having the name $HOST
$json = $ua->get( "https://api.linode.com/v4/domains/$domain_id/records" )->content;
+
$records = $ua->get( "https://api.linode.com/v4/domains/$domain_id/records" )->content;
for ( @{ decode_json( $json )->{data} } ) {
+
for ( @{ decode_json( $records )->{data} } ) {
$record_id = $_->{id} if $_->{name} eq $host;
+
$record_id = $_->{id} if $_->{name} eq $HOST;
 
}
 
}
die "Host name not found" unless $record_id;
 
  
 
# Update the A record
 
# Update the A record
 
$ua->put(
 
$ua->put(
 
"https://api.linode.com/v4/domains/$domain_id/records/$record_id",
 
"https://api.linode.com/v4/domains/$domain_id/records/$record_id",
'Content' => encode_json({ 'target' => $new_IP })
+
'Content' => encode_json({ 'target' => $IP })
 
);
 
);
 
</source>
 
</source>

Revision as of 13:45, 28 July 2023

Cone.png This article or section is a stub. Stubs are articles that have not yet received substantial attention from the authors. They are short or insufficient pieces of information and require additions to further increase the article's usefulness. The project values stubs as useful first steps toward complete articles.


Installing the linode CLI:

pip3 install linode-cli
linode-cli configure --token
linode-cli --json --pretty domains list


Example Perl code using LWP to update an A record $HOST.$DOMAIN to $IP:

# Set up a user agent with our token as default header
$ua = LWP::UserAgent->new();
$ua->default_header(
	'Content-Type'  => "application/json",
	'Authorization' => "Bearer $API_KEY"
);

# Find the ID $DOMAIN
$domains = $ua->get( 'https://api.linode.com/v4/domains' )->content;
for ( @{ decode_json( $domains )->{data} } ) {
	$domain_id = $_->{id} if $_->{domain} eq $DOMAIN;
}

# Find the ID of the A record having the name $HOST
$records = $ua->get( "https://api.linode.com/v4/domains/$domain_id/records" )->content;
for ( @{ decode_json( $records )->{data} } ) {
	$record_id = $_->{id} if $_->{name} eq $HOST;
}

# Update the A record
$ua->put(
	"https://api.linode.com/v4/domains/$domain_id/records/$record_id",
	'Content' => encode_json({ 'target' => $IP })
);